package org.tgcloud.group.service.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.tgcloud.group.core.*;
import org.tgcloud.group.entity.*;
import org.tgcloud.group.repository.*;
import org.tgcloud.group.web.controller.dto.home.*;
import org.tgcloud.group.web.controller.dto.order.TreeOrderListDTO;
import org.tgcloud.group.web.controller.dto.sys.SysLabelValueListDTO;
import org.tgcloud.group.web.controller.dto.user.BusinessLevelDTO;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * @author: zhy
 * @create: 2023-09-26 14:54
 * @description:
 */
@Service
public class HomeManageService {

    @Autowired
    private MjOrderMapper orderMapper;

    @Autowired
    private MjUserOrderMapper userOrderMapper;

    @Autowired
    private MjOrderBlindBoxMapper orderBlindBoxMapper;

    @Autowired
    private SysLabelValueMapper sysLabelValueMapper;

    @Autowired
    private MjProjectMapper projectMapper;

    @Autowired
    private MjPayrollMapper payrollMapper;

    @Autowired
    private MjBusinessPersonMapper businessPersonMapper;

    @Autowired
    private MjBusinessTimePriceMapper businessTimePriceMapper;

    @Autowired
    private BusinessManageService businessManageService;

    @Autowired
    private MjProjectBatchMapper projectBatchMapper;

    @Autowired
    private OrderManagerService orderManagerService;

    public APPResultMessage doSendOrder(Date startTime, Date endTime) {
        // checkPrice 签单总额
        // goodPrice 卖货总额
        // blindPrice 盲盒变现总额
        Map map = new HashMap();
        Map<String, BigDecimal> monthMap = new HashMap();
        Map<String, BigDecimal> yearMap = new HashMap();
        List<MjOrder> monthOrders = orderMapper.selectMonthHomeListByStartTimeAndEndTime(startTime, endTime);
        for (MjOrder order : monthOrders){
            List<MjUserOrder> mjUserOrders = userOrderMapper.selectAddListByOrderIdAndOrderType(order.getId(), (short) 1);
            for (MjUserOrder userOrder : mjUserOrders){
                if (monthMap.containsKey("checkPrice")){
                    monthMap.put("checkPrice", userOrder.getTotalPrice().add(monthMap.get("checkPrice")));
                }else {
                    monthMap.put("checkPrice", userOrder.getTotalPrice());
                }
            }
            //盲盒与卖货
            MjUserOrder mjUserOrder = userOrderMapper.selectIsHaveByOrderIdAndUserIdAndOrderType(null, order.getId(), (short) 4);
            if (mjUserOrder == null){
                mjUserOrder = userOrderMapper.selectMsgByOrderIdAndOrderTypeAndRankNum(order.getId(), (short) 3, 1);
                if (mjUserOrder == null){
                    mjUserOrder = userOrderMapper.selectMsgByOrderIdAndOrderTypeAndRankNum(order.getId(), (short) 2, 1);
                }
                monthMap.put("goodPrice", mjUserOrder.getTotalPrice());
                List<MjOrderBlindBox> orderBlindBoxes = orderBlindBoxMapper.selectGoodsByOrderId(order.getId());
                for (MjOrderBlindBox box : orderBlindBoxes){
                    if (monthMap.containsKey("blindPrice")){
                        monthMap.put("blindPrice", box.getPrice().multiply(new BigDecimal(box.getNum())).add(monthMap.get("blindPrice")));
                    }else {
                        monthMap.put("blindPrice", box.getPrice().multiply(new BigDecimal(box.getNum())));
                    }
                }
            }else {
                List<MjUserOrder> userOrders = userOrderMapper.selectAddListByOrderIdAndOrderType(order.getId(), (short) 4);
                for (MjUserOrder userOrder : userOrders){
                    if (monthMap.containsKey("goodPrice")){
                        monthMap.put("goodPrice", userOrder.getTotalPrice().add(monthMap.get("goodPrice")));
                    }else {
                        monthMap.put("goodPrice", userOrder.getTotalPrice());
                    }
                }
                List<MjUserOrder> userOrders5 = userOrderMapper.selectAddListByOrderIdAndOrderType(order.getId(), (short) 5);
                for (MjUserOrder userOrder : userOrders5){
                    if (monthMap.containsKey("blindPrice")){
                        monthMap.put("blindPrice", userOrder.getTotalPrice().add(monthMap.get("blindPrice")));
                    }else {
                        monthMap.put("blindPrice", userOrder.getTotalPrice());
                    }
                }
            }
        }
        List<MjOrder> yearOrders = orderMapper.selectYearHomeListByStartTimeAndEndTime(startTime, endTime);
        map.put("month", monthMap);
        map.put("year", yearMap);
        List<SysLabelValueListDTO> sysLabelValueListDTOS = sysLabelValueMapper.selectListBySysLabelId(10, null,0);
        map.put("yearTarget", sysLabelValueListDTOS.get(0));
        map.put("monthTarget", sysLabelValueListDTOS.get(2));
        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setData(map);
        successMessage.setResult(resultMessage);
        return successMessage;
    }


    // 计算月份统计
    // 预估利润=预估应收-预估成本-所有人员支出
    public APPResultMessage getHomeMonth(Date time){
        HomeProjectTotal projectTotal = new HomeProjectTotal();
        List<MjProject> mjProjects = projectMapper.selectHomeListByTime(time);
        for (MjProject project : mjProjects){
            List<MjOrder> orders = orderMapper.selectListByProjectId(project.getId());
            if (BeanUtil.isNullList(orders)){
                projectTotal.setSignTotalPrice(projectTotal.getSignTotalPrice() == null ? BigDecimal.ZERO : projectTotal.getSignTotalPrice());
                projectTotal.setInnerTotalPrice(projectTotal.getInnerTotalPrice() == null? BigDecimal.ZERO : projectTotal.getInnerTotalPrice());
                projectTotal.setSaleBlindPrice(projectTotal.getSaleBlindPrice() == null? BigDecimal.ZERO : projectTotal.getSaleBlindPrice());
                projectTotal.setNoBlindPrice(projectTotal.getNoBlindPrice() == null? BigDecimal.ZERO : projectTotal.getNoBlindPrice());
                projectTotal.setEstimatedCost(projectTotal.getEstimatedCost() == null? BigDecimal.ZERO : projectTotal.getEstimatedCost());
                projectTotal.setExpectGrossProfit(projectTotal.getExpectGrossProfit() == null? BigDecimal.ZERO : projectTotal.getExpectGrossProfit());
            }else {
                for (MjOrder order : orders){
                    //签单总额
                    projectTotal.setSignTotalPrice((projectTotal.getSignTotalPrice() == null ? BigDecimal.ZERO : projectTotal.getSignTotalPrice()).add(order.getTotalPrice() == null ? BigDecimal.ZERO : order.getTotalPrice()));
                    // 内部货值
                    projectTotal.setInnerTotalPrice((projectTotal.getInnerTotalPrice() == null ? BigDecimal.ZERO : projectTotal.getInnerTotalPrice()).add(order.getInnerPrice() == null ? BigDecimal.ZERO : order.getInnerPrice()));
                    // 预估应收
                    projectTotal.setSaleBlindPrice((projectTotal.getSaleBlindPrice() == null ? BigDecimal.ZERO : projectTotal.getSaleBlindPrice()).add(order.getCheckCashPrice() == null ? BigDecimal.ZERO : order.getCheckCashPrice()).add(order.getBlindBoxPrice() == null ? BigDecimal.ZERO : order.getBlindBoxPrice()));
                    // 去盲盒应收
                    projectTotal.setNoBlindPrice((projectTotal.getNoBlindPrice() == null ? BigDecimal.ZERO : projectTotal.getNoBlindPrice()).add(order.getCheckCashPrice() == null ? BigDecimal.ZERO : order.getCheckCashPrice()));
                    // 预估成本
                    projectTotal.setEstimatedCost((projectTotal.getEstimatedCost() == null ? BigDecimal.ZERO : projectTotal.getEstimatedCost()).add(order.getEstimatedCost() == null ? BigDecimal.ZERO : order.getEstimatedCost()));
                    // 预估毛利=卖货应收+盲盒应收-项目成本(预估成本)
                    BigDecimal price1 = (order.getCheckCashPrice() == null ? BigDecimal.ZERO : order.getCheckCashPrice()).add(order.getBlindBoxPrice() == null ? BigDecimal.ZERO : order.getBlindBoxPrice()).subtract(order.getEstimatedCost() == null ? BigDecimal.ZERO : order.getEstimatedCost());
                    projectTotal.setExpectGrossProfit((projectTotal.getExpectGrossProfit() == null ? BigDecimal.ZERO : projectTotal.getExpectGrossProfit()).add(price1));
                }
            }
        }
        // 查询所有人支出以及所有商务支出
        BigDecimal businessPrice = payrollMapper.selectAllPayPriceByTime(time, (short) 1);
        projectTotal.setAllBusinessOutPrice(businessPrice == null ? BigDecimal.ZERO : businessPrice);
        BigDecimal allPersonPrice = payrollMapper.selectAllPayPriceByTime(time, null);
        projectTotal.setAllPersonOutPrice(allPersonPrice == null ? BigDecimal.ZERO : allPersonPrice);
        // 预估利润=预估应收-预估成本-所有人员支出
        projectTotal.setRateTotalPrice((projectTotal.getSaleBlindPrice() == null ? BigDecimal.ZERO : projectTotal.getSaleBlindPrice()).subtract(projectTotal.getEstimatedCost() == null ? BigDecimal.ZERO : projectTotal.getEstimatedCost()).subtract(projectTotal.getAllPersonOutPrice() == null ? BigDecimal.ZERO : projectTotal.getAllPersonOutPrice()));
        projectTotal.setNoBusinessRAate((projectTotal.getExpectGrossProfit() == null ? BigDecimal.ZERO : projectTotal.getExpectGrossProfit()).subtract(projectTotal.getAllBusinessOutPrice()));

        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setData(projectTotal);
        successMessage.setResult(resultMessage);
        return successMessage;
    }


    // 计算年度签单总额
    public APPResultMessage getHomeYear(Date time){
        HomeProjectYearTotal projectTotal = new HomeProjectYearTotal();
        String s = DateUtil.getyyyyByDate(time);
        List<SysLabelValueListDTO> sysLabelValueListDTOS = sysLabelValueMapper.selectListBySysLabelId(10, s,0);
        //年度目标
        BigDecimal yearTotalPrice = BigDecimal.ZERO;
        //年度项目收入
        BigDecimal yearProjectPrice = BigDecimal.ZERO;
        //年度项目成本
        BigDecimal yearProjectCost = BigDecimal.ZERO;
        //年度盲盒收入
        BigDecimal yearBlindPrice = BigDecimal.ZERO;
        //年度签单总额
        BigDecimal nowYearTotalPrice = BigDecimal.ZERO;
        //年度内部货值
        BigDecimal nowYearInnerPrice = BigDecimal.ZERO;


        if (BeanUtil.isNullList(sysLabelValueListDTOS)){
            SysLabelValue sysLabelValue = sysLabelValueMapper.selectByPrimaryKey(23);
            yearTotalPrice = new BigDecimal(sysLabelValue.getSysValueName());
        }else {
            yearTotalPrice = new BigDecimal(sysLabelValueListDTOS.get(0).getSysValueName());
        }
        projectTotal.setPlanSignYearTotalPrice(yearTotalPrice);
        List<MjProject> mjProjects = projectMapper.selectHomeListByYearTime(time);
        for (MjProject project : mjProjects){
            List<MjOrder> orders = orderMapper.selectListByProjectId(project.getId());
            if (BeanUtil.isNullList(orders)){
                projectTotal.setInnerYearTotalPrice(projectTotal.getInnerYearTotalPrice() == null ? BigDecimal.ZERO : projectTotal.getInnerYearTotalPrice());
                projectTotal.setSignYearTotalPrice(projectTotal.getSignYearTotalPrice() == null ? BigDecimal.ZERO : projectTotal.getSignYearTotalPrice());
            }else {
                for (MjOrder order : orders){
                    //先不找子订单，等查到父订单再计算
                    if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                        //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                        if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                            //子订单卖货实收
                            BigDecimal orderSealRealPrice = BigDecimal.ZERO;
                            //子订单盲盒收入
                            BigDecimal orderBlindBoxPrice = BigDecimal.ZERO;
                            List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                            if (CollUtil.isNotEmpty(childOrderList)){
                                for (TreeOrderListDTO childOrder : childOrderList) {
                                    //年度内部货值
                                    if (projectTotal.getInnerYearTotalPrice() != null){
                                        nowYearInnerPrice = nowYearInnerPrice.add(childOrder.getInnerPrice() == null ? BigDecimal.ZERO : childOrder.getInnerPrice());
                                    } else {
                                        projectTotal.setInnerYearTotalPrice(BigDecimal.ZERO);
                                    }
                                    //年度签单总额
                                    if (projectTotal.getSignYearTotalPrice() != null){
                                        nowYearTotalPrice = nowYearTotalPrice.add(childOrder.getTotalPrice() == null ? BigDecimal.ZERO : childOrder.getTotalPrice());
                                    } else {
                                        projectTotal.setSignYearTotalPrice(BigDecimal.ZERO);
                                    }
                                    // 项目成本 ： 有实际成本用实际，没有用预计
                                    if (childOrder.getActualCost() != null && childOrder.getActualCost().compareTo(BigDecimal.ZERO) != 0){
                                        yearProjectCost = yearProjectCost.add(childOrder.getActualCost());
                                    } else {
                                        if (childOrder.getEstimatedCost() != null){
                                            yearProjectCost = yearProjectCost.add(childOrder.getEstimatedCost());
                                        }
                                    }
                                    //子订单盲盒收入
                                    if (ObjUtil.isNotNull(childOrder.getExpectPrice()) && childOrder.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                                        //说明走的盲盒
                                        orderBlindBoxPrice = orderBlindBoxPrice.add(childOrder.getExpectPrice());
                                    } else if (childOrder.getTotalNum() != null && childOrder.getIsBlindBox() == 2){

                                            orderBlindBoxPrice = orderBlindBoxPrice.add(new BigDecimal(childOrder.getTotalNum()).multiply(new BigDecimal(9)));
                                    } else {
                                        //子订单卖货实收
                                        orderSealRealPrice = orderSealRealPrice.add(childOrder.getSaleRealPrice() != null ? childOrder.getSaleRealPrice() : BigDecimal.ZERO);
                                    }

                                }
                            }
                            //盲盒收入+卖货收入（实际/核价--核价用父--实际用子）
                            yearProjectPrice = yearProjectPrice.add(orderBlindBoxPrice).add(orderSealRealPrice.compareTo(BigDecimal.ZERO) == 0 ? (order.getCheckCashPrice() != null ? order.getCheckCashPrice() : BigDecimal.ZERO) : orderSealRealPrice);
                            yearBlindPrice = yearBlindPrice.add(orderBlindBoxPrice);
                        } else {
                            //年度内部货值
                            if (projectTotal.getInnerYearTotalPrice() != null){
                                nowYearInnerPrice = nowYearInnerPrice.add(order.getInnerPrice() == null ? BigDecimal.ZERO : order.getInnerPrice());
                            } else {
                                projectTotal.setInnerYearTotalPrice(BigDecimal.ZERO);
                            }
                            //年度签单总额
                            if (projectTotal.getSignYearTotalPrice() != null){
                                nowYearTotalPrice = nowYearTotalPrice.add(order.getTotalPrice() == null ? BigDecimal.ZERO : order.getTotalPrice());
                            } else {
                                projectTotal.setSignYearTotalPrice(BigDecimal.ZERO);
                            }

                            //方案四 ：项目收入：盲盒应收（expectPrice/num*9）+（卖货实收/核价应收）

                            //盲盒收入
                            if (ObjUtil.isNotNull(order.getExpectPrice()) && order.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                                yearProjectPrice = yearProjectPrice.add(order.getExpectPrice());
                                yearBlindPrice = yearBlindPrice.add(order.getExpectPrice());
                            } else if (order.getTotalNum() != null && order.getIsBlindBox() == 2){
                                yearProjectPrice = yearProjectPrice.add(new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(9)));
                                yearBlindPrice = yearBlindPrice.add(new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(9)));
                            } else {
                                //卖货收入
                                if (order.getSaleRealPrice() != null && order.getSaleRealPrice().compareTo(BigDecimal.ZERO) != 0){
                                    yearProjectPrice = yearProjectPrice.add(order.getSaleRealPrice());
                                } else {
                                    yearProjectPrice = yearProjectPrice.add(order.getCheckCashPrice() != null ? order.getCheckCashPrice() : BigDecimal.ZERO);
                                }
                            }
                            // 项目成本 ： 有实际成本用实际，没有用预计
                            if (order.getActualCost() != null && order.getActualCost().compareTo(BigDecimal.ZERO) != 0){
                                yearProjectCost = yearProjectCost.add(order.getActualCost());
                            } else {
                                if (order.getEstimatedCost() != null){
                                    yearProjectCost = yearProjectCost.add(order.getEstimatedCost());
                                }
                            }
                        }
                    }

                }
            }
        }
        //盲盒收入
        projectTotal.setProjectBiddingPrice(yearBlindPrice);
        //年度内部货值
        projectTotal.setInnerYearTotalPrice(nowYearInnerPrice);
        //年度签单总额
        projectTotal.setSignYearTotalPrice(nowYearTotalPrice);
        //年度收入
        projectTotal.setProjectPrice(yearProjectPrice);
        //年度成本
        projectTotal.setProjectCost(yearProjectCost);
        //去盲盒利润    项目收入-盲盒收入-项目成本
        projectTotal.setProjectNoBiddingRateGross(projectTotal.getProjectPrice().subtract(yearBlindPrice).subtract(projectTotal.getProjectCost()));
        //去盲盒收入 项目收入-盲盒收入
        projectTotal.setProjectNoBiddingPrice(projectTotal.getProjectPrice().subtract(projectTotal.getProjectBiddingPrice()));
        //项目利润  收入-成本
        projectTotal.setProjectGross(projectTotal.getProjectPrice().subtract(projectTotal.getProjectCost()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String format = sdf.format(time);
        if (format.equals("2023")){
            //年度签单总额
            projectTotal.setSignYearTotalPrice(projectTotal.getSignYearTotalPrice().add(new BigDecimal(374568261.45)));
            //年度内部货值
            projectTotal.setInnerYearTotalPrice(projectTotal.getInnerYearTotalPrice().add(new BigDecimal(197868650.22)));
        }
        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setData(projectTotal);
        successMessage.setResult(resultMessage);
        return successMessage;
    }


    public APPResultMessage getHomeSignProject(Date time, Date endTime, Date startArrivalTime, Date endArrivalTime, Integer pageNum, Integer pageSize, Integer useType,
                                               String businessIds, Integer areaId, String invoiceCompany,
                                               String projectName, String brands, String areaIds, String statuses,
                                               String arrivalProgressIds, String invoiceCompanyIds, String orderNo, String cateIds,
                                               String innerPriceSearches, String actualValuePriceSearches, String saleRealPriceSearches,
                                               String estimatedCostSearches, String actualCostSearches, String arrivalRateSearches,
                                               BigDecimal startProjectRateGross, BigDecimal endProjectRateGross,
                                               String estimatedSelfMediaCostSearches, String estimatedExternalMediaCostSearches,
                                               String actualSelfMediaCostSearches, String actualExternalMediaCostSearches) {
        String[] businessArr = null;
        if (StringUtil.isNotBlank(businessIds)){
            businessArr = businessIds.split(",");
        }
        String[] areaIdList = null;
        if (StringUtil.isNotBlank(areaIds)){
            areaIdList = areaIds.split(",");
        }
        String[] statusList = null;
        if (StringUtil.isNotBlank(statuses)){
            statusList = statuses.split(",");
        }
        String[] arrivalProgressIdList = null;
        if (StringUtil.isNotBlank(arrivalProgressIds)){
            arrivalProgressIdList = arrivalProgressIds.split(",");
        }
        String[] invoiceCompanyIdList = null;
        if (StringUtil.isNotBlank(invoiceCompanyIds)){
            invoiceCompanyIdList = invoiceCompanyIds.split(",");
        }
        if (startArrivalTime != null && endArrivalTime != null){
            time = null;
            endTime = null;
        }
        String[] cateIdList = null;
        if (StringUtil.isNotBlank(cateIds)){
            cateIdList = cateIds.trim().split(",");
        }
        String[] innerPriceSearchArr = null;
        if (StringUtil.isNotBlank(innerPriceSearches)){
            innerPriceSearchArr = innerPriceSearches.split(",");
        }
        String[] actualValuePriceSearchArr = null;
        if (StringUtil.isNotBlank(actualValuePriceSearches)){
            actualValuePriceSearchArr = actualValuePriceSearches.split(",");
        }
        String[] saleRealPriceSearchArr = null;
        if (StringUtil.isNotBlank(saleRealPriceSearches)){
            saleRealPriceSearchArr = saleRealPriceSearches.split(",");
        }
        String[] estimatedCostSearchArr = null;
        if (StringUtil.isNotBlank(estimatedCostSearches)){
            estimatedCostSearchArr = estimatedCostSearches.split(",");
        }
        String[] estimatedSelfMediaCostSearchArr = null;
        if (StringUtil.isNotBlank(estimatedSelfMediaCostSearches)){
            estimatedSelfMediaCostSearchArr = estimatedSelfMediaCostSearches.split(",");
        }
        String[] estimatedExternalMediaCostSearchArr = null;
        if (StringUtil.isNotBlank(estimatedExternalMediaCostSearches)){
            estimatedExternalMediaCostSearchArr = estimatedExternalMediaCostSearches.split(",");
        }
        String[] actualSelfMediaCostSearchArr = null;
        if (StringUtil.isNotBlank(actualSelfMediaCostSearches)){
            actualSelfMediaCostSearchArr = actualSelfMediaCostSearches.split(",");
        }
        String[] actualExternalMediaCostSearchArr = null;
        if (StringUtil.isNotBlank(actualExternalMediaCostSearches)){
            actualExternalMediaCostSearchArr = actualExternalMediaCostSearches.split(",");
        }
        String[] actualCostSearchArr = null;
        if (StringUtil.isNotBlank(actualCostSearches)){
            actualCostSearchArr = actualCostSearches.split(",");
        }
        String[] arrivalRateSearchArr = null;
        if (StringUtil.isNotBlank(arrivalRateSearches)){
            arrivalRateSearchArr = arrivalRateSearches.split(",");
        }
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<HomeProjectMsg> list = projectMapper.selectHomeSignProjectListByTime(time, endTime, startArrivalTime,endArrivalTime,businessArr, areaId, invoiceCompany,
                    projectName, brands, areaIdList, statusList, arrivalProgressIdList, invoiceCompanyIdList,orderNo,cateIdList,
                    innerPriceSearchArr, actualValuePriceSearchArr, saleRealPriceSearchArr, estimatedCostSearchArr, actualCostSearchArr,
                    arrivalRateSearchArr, estimatedSelfMediaCostSearchArr, estimatedExternalMediaCostSearchArr,
                    actualSelfMediaCostSearchArr, actualExternalMediaCostSearchArr);
            PageInfo<HomeProjectMsg> tables = new PageInfo<>(list);
            for (HomeProjectMsg homeProjectMsg : tables.getList()) {
                //品类名称
                if (StringUtil.isNotBlank(homeProjectMsg.getCateIds())){
                    String cateId = homeProjectMsg.getCateIds();
                    String name = "";
                    for (String id : cateId.split(",")){
                        SysLabelValue labelValue = sysLabelValueMapper.selectByPrimaryKey(Integer.valueOf(id));
                        name = name + labelValue.getSysValueName() + ",";
                    }
                    homeProjectMsg.setCateNames(name);
                }
                //计算本段时间内的到货内部货值
                if (homeProjectMsg.getArrivalRate() != null ){
                    homeProjectMsg.setArrivalInnerPrice(homeProjectMsg.getInnerPrice() == null ? BigDecimal.ZERO :
                            homeProjectMsg.getInnerPrice().multiply(homeProjectMsg.getArrivalRate()).multiply(new BigDecimal(0.01)));
                    homeProjectMsg.setArrivalRate(homeProjectMsg.getArrivalRate());
                } else {
                    homeProjectMsg.setArrivalRate(BigDecimal.ZERO);
                    homeProjectMsg.setArrivalInnerPrice(BigDecimal.ZERO);
                }
            }
            List<HomeProjectMsg> homeProjectList = doGetProjectSomeTotal(tables.getList(), useType);
            List<HomeProjectMsg> finalList = new ArrayList<>();
            for (HomeProjectMsg msg : homeProjectList) {
                if (startProjectRateGross != null || endProjectRateGross != null){
                    boolean addFinalMsg = false;
                    if (msg.getProjectRateGross() != null){
                        if (startProjectRateGross != null && msg.getProjectRateGross().compareTo(startProjectRateGross) >= 0){
                            addFinalMsg = true;
                        }
                        if (endProjectRateGross != null){
                            if (msg.getProjectRateGross().compareTo(endProjectRateGross) <= 0){
                                addFinalMsg = true;
                            }else {
                                addFinalMsg = false;
                            }
                        }
                    }
                    if (addFinalMsg){
                        finalList.add(msg);
                    }
                }else {
                    finalList.add(msg);
                }
            }
            tables.setList(finalList);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            result.setData(getProjectTotalCount(list, useType));
            APPResultMessage<Object, HomeProjectMsg> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<HomeProjectMsg> list = projectMapper.selectHomeSignProjectListByTime(time, endTime,  startArrivalTime,endArrivalTime,businessArr, areaId, invoiceCompany,
                    projectName, brands, areaIdList, statusList, arrivalProgressIdList, invoiceCompanyIdList,orderNo,cateIdList,
                    innerPriceSearchArr, actualValuePriceSearchArr, saleRealPriceSearchArr, estimatedCostSearchArr, actualCostSearchArr,
                    arrivalRateSearchArr, estimatedSelfMediaCostSearchArr, estimatedExternalMediaCostSearchArr,
                    actualSelfMediaCostSearchArr, actualExternalMediaCostSearchArr);
            for (HomeProjectMsg homeProjectMsg : list) {
                //品类名称
                if (StringUtil.isNotBlank(homeProjectMsg.getCateIds())){
                    String cateId = homeProjectMsg.getCateIds();
                    String name = "";
                    for (String id : cateId.split(",")){
                        SysLabelValue labelValue = sysLabelValueMapper.selectByPrimaryKey(Integer.valueOf(id));
                        name = name + labelValue.getSysValueName() + ",";
                    }
                    homeProjectMsg.setCateNames(name);
                }
                //计算本段时间内的到货内部货值
                if (homeProjectMsg.getArrivalRate() != null ){
                    homeProjectMsg.setArrivalInnerPrice(homeProjectMsg.getInnerPrice() == null ? BigDecimal.ZERO :
                            homeProjectMsg.getInnerPrice().multiply(homeProjectMsg.getArrivalRate()).multiply(new BigDecimal(0.01)));
                    homeProjectMsg.setArrivalRate(homeProjectMsg.getArrivalRate());
                } else {
                    homeProjectMsg.setArrivalRate(BigDecimal.ZERO);
                    homeProjectMsg.setArrivalInnerPrice(BigDecimal.ZERO);
                }
            }
            list = doGetProjectSomeTotal(list, useType);
            List<HomeProjectMsg> finalList = new ArrayList<>();
            for (HomeProjectMsg msg : list) {
                if (startProjectRateGross != null || endProjectRateGross != null){
                    boolean addFinalMsg = false;
                    if (msg.getProjectRateGross() != null){
                        if (startProjectRateGross != null && msg.getProjectRateGross().compareTo(startProjectRateGross) >= 0){
                            addFinalMsg = true;
                        }
                        if (endProjectRateGross != null){
                            if (msg.getProjectRateGross().compareTo(endProjectRateGross) <= 0){
                                addFinalMsg = true;
                            }else {
                                addFinalMsg = false;
                            }
                        }
                    }
                    if (addFinalMsg){
                        finalList.add(msg);
                    }
                }else {
                    finalList.add(msg);
                }
            }
            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage result = ResultMessage.builder().setList(finalList);
            result.setData(getProjectTotalCount(finalList,useType));
            successMessage.setResult(result);
            return successMessage;
        }
    }


    public List<HomeProjectMsg> doGetProjectSomeTotal(List<HomeProjectMsg> list, Integer useType) {
        List<SysLabelValueListDTO> valueListDTOS = sysLabelValueMapper.selectListBySysLabelId(42, null, null);
        String blindOnePrice = sysLabelValueMapper.selectValueBySysIdAndReadOne(9);
        for (HomeProjectMsg msg : list){
            Map<Integer, BigDecimal> useToPriceMap = new HashMap();
            for (SysLabelValueListDTO dto : valueListDTOS){
                useToPriceMap.put(dto.getId(), BigDecimal.ZERO);
            }
            // 实际货值
            BigDecimal actualValuePrice = BigDecimal.ZERO;
            // 核价卖货应收
            BigDecimal checkSalePrice = BigDecimal.ZERO;
            // 竞价卖货应收
            BigDecimal biddingSalePrice = BigDecimal.ZERO;
            BigDecimal saleRealPrice = BigDecimal.ZERO;  // 卖货实收
            // 盲盒收入/盲盒应收
            BigDecimal blindBoxPriceFinal = BigDecimal.ZERO;
            //盲盒实收
            BigDecimal blindRealPriceFinal = BigDecimal.ZERO;
            //项目应收 = 订单去向为渠道竞价的卖货实收 + 其他的订单去向的预计变现
            BigDecimal projectSalePriceFinal = BigDecimal.ZERO;
            //卖货实收 = 订单去向为渠道竞价的卖货实收之和
            BigDecimal projectSaleRealPriceFinal = BigDecimal.ZERO;
            BigDecimal projectOtherRealPriceFinal = BigDecimal.ZERO;
            //预计成本
            BigDecimal estimateCostFinal = BigDecimal.ZERO;
            //实际成本
            BigDecimal actualCostFinal = BigDecimal.ZERO;
            //开单总额/项目总货值
            BigDecimal totalPriceFinal = BigDecimal.ZERO;
            //内部货值
            BigDecimal innerPriceFinal = BigDecimal.ZERO;
            //项目实际货值
            BigDecimal projectRealTotalPriceFinal = BigDecimal.ZERO;
            //总成本-根据useType变动
            BigDecimal useTypeTotalCost = BigDecimal.ZERO;
            //总收入-根据useType变动
            BigDecimal useTypeTotalPrice = BigDecimal.ZERO;
            //盲盒收入-根据useType变动
            BigDecimal useTypeBlindTotalPrice = BigDecimal.ZERO;
            //盲盒成本-根据useType变动
            BigDecimal useTypeBlindTotalCost = BigDecimal.ZERO;

            List<MjOrder> orders = orderMapper.selectNoSubListByProjectId(msg.getProjectId());
            for (MjOrder order : orders){

                // 核价卖货应收
                BigDecimal checkOrderPrice = order.getCheckCashPrice();
                // 盲盒收入/盲盒应收
                BigDecimal blindBoxPrice = BigDecimal.ZERO;
                //盲盒实收
                BigDecimal blindRealPrice = BigDecimal.ZERO;
                //项目应收 = 订单去向为渠道竞价的卖货实收 + 其他的订单去向的预计变现
                BigDecimal projectSalePrice = BigDecimal.ZERO;
                //卖货实收 = 订单去向为渠道竞价的卖货实收之和
                BigDecimal projectSaleRealPrice = BigDecimal.ZERO;
                //预计成本
                BigDecimal estimateCost = BigDecimal.ZERO;
                //实际成本
                BigDecimal actualCost = BigDecimal.ZERO;
                //开单总额/项目总货值
                BigDecimal totalPrice = BigDecimal.ZERO;
                //内部货值
                BigDecimal innerPrice = BigDecimal.ZERO;
                //项目实际货值
                BigDecimal projectRealTotalPrice = BigDecimal.ZERO;
                // 盲盒-预计成本
                BigDecimal blindBoxCost = BigDecimal.ZERO;
                //盲盒-实际成本
                BigDecimal blindBoxRealCost = BigDecimal.ZERO;
                // 是否存在易货表单
                boolean hasYiHuoOrder = false;

                //先不查子订单，等查到父订单再计算
                if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                    //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                    if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                        List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                        if (CollUtil.isNotEmpty(childOrderList)){
                            for (TreeOrderListDTO childOrder : childOrderList) {
                                if (ObjUtil.isNotNull(childOrder.getExpectPrice()) && childOrder.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                                    if (ObjUtil.isNotNull(childOrder.getOrderGo()) ){
                                        useToPriceMap.put(childOrder.getOrderGo(), childOrder.getExpectPrice().add(useToPriceMap.get(childOrder.getOrderGo())));
                                    }else {
                                        useToPriceMap.put(155, childOrder.getExpectPrice().add(useToPriceMap.get(155)));
                                    }
                                } else {
                                    if (childOrder.getIsBlindBox() == 2){
                                        BigDecimal blindBoxOneTotalPrice = childOrder.getTotalNum() != null ? new BigDecimal(childOrder.getTotalNum()).multiply(new BigDecimal(blindOnePrice)) : BigDecimal.ZERO;
                                        useToPriceMap.put(5047, blindBoxOneTotalPrice.add(useToPriceMap.get(5047)));
                                    }else {
                                        useToPriceMap.put(155, childOrder.getTotalPrice().add(useToPriceMap.get(155)));
                                    }
                                }
                                if (childOrder.getOrderGo() != null && childOrder.getOrderGo() == 5047){
                                    // 盲盒
                                    //盲盒收入
                                    if (childOrder.getExpectPrice() != null && childOrder.getExpectPrice().compareTo(BigDecimal.ZERO) > 0){
                                        blindBoxPrice = blindBoxPrice.add(childOrder.getExpectPrice());
                                    }else {
                                        if (childOrder.getTotalNum() != null) {
                                            BigDecimal bTotalPrice = new BigDecimal(childOrder.getTotalNum()).multiply(new BigDecimal(blindOnePrice));
                                            blindBoxPrice = blindBoxPrice.add(bTotalPrice);
                                        }
                                    }
                                    //盲盒实收
                                    blindRealPrice = blindRealPrice.add(childOrder.getBlindRealPrice() != null ? childOrder.getBlindRealPrice() : BigDecimal.ZERO);
                                    // 盲盒-预计成本
                                    blindBoxCost = blindBoxCost.add(childOrder.getEstimatedCost() != null ? childOrder.getEstimatedCost() : BigDecimal.ZERO);
                                    //盲盒-实际成本
                                    blindBoxRealCost = blindBoxRealCost.add(childOrder.getActualCost() != null ? childOrder.getActualCost() : BigDecimal.ZERO);
                                }else {
                                    // 其他全部算在易货表单里
                                    //项目应收 = 订单去向为渠道竞价的卖货实收 + 其他的订单去向的预计变现
                                    // （这里没有盲盒应收，最后结算的时候需要手动加上盲盒应收的数据）
                                    if (childOrder.getOrderGo() != null && childOrder.getOrderGo() == 155){
                                        hasYiHuoOrder = true;
                                        projectSalePrice = projectSalePrice.add(childOrder.getSaleRealPrice() != null ? childOrder.getSaleRealPrice() : BigDecimal.ZERO);
//                                    }else {
//                                        projectSalePrice = projectSalePrice.add(childOrder.getExpectPrice() != null && childOrder.getExpectPrice().compareTo(BigDecimal.ZERO) > 0 ? childOrder.getExpectPrice() : BigDecimal.ZERO);
                                    }
                                    //卖货实收 = 订单去向为渠道竞价的卖货实收之和
                                    if (childOrder.getOrderGo() != null && childOrder.getOrderGo() == 155){
                                        projectSaleRealPrice = projectSaleRealPrice.add(childOrder.getSaleRealPrice() != null ? childOrder.getSaleRealPrice() : BigDecimal.ZERO);
                                    }else {
                                        projectOtherRealPriceFinal = projectOtherRealPriceFinal.add(childOrder.getExpectPrice() != null && childOrder.getExpectPrice().compareTo(BigDecimal.ZERO) > 0 ? childOrder.getExpectPrice() : BigDecimal.ZERO);
                                    }
                                }
                                // 预计成本
                                estimateCost = estimateCost.add(childOrder.getEstimatedCost() != null ? childOrder.getEstimatedCost() : BigDecimal.ZERO);
                                // 实际成本
                                actualCost = actualCost.add(childOrder.getActualCost() != null ? childOrder.getActualCost() : BigDecimal.ZERO);
                                // 开单总额/项目总货值
                                totalPrice = totalPrice.add(childOrder.getTotalPrice() != null ? childOrder.getTotalPrice() : BigDecimal.ZERO);
                                // 内部货值
                                innerPrice = innerPrice.add(childOrder.getInnerPrice() != null ? childOrder.getInnerPrice() : BigDecimal.ZERO);
                                // 项目实际货值
                                projectRealTotalPrice = projectRealTotalPrice.add(childOrder.getRealTotalPrice() != null ? childOrder.getRealTotalPrice() : BigDecimal.ZERO);

                                actualValuePrice = actualValuePrice.add(childOrder.getRealTotalPrice() != null ? childOrder.getRealTotalPrice() : BigDecimal.ZERO);
                                biddingSalePrice = biddingSalePrice.add(childOrder.getBiddingTwoTopPrice() != null ? childOrder.getBiddingTwoTopPrice() : childOrder.getBiddingOneFirstPrice() != null ? childOrder.getBiddingOneFirstPrice() : BigDecimal.ZERO);
                                saleRealPrice = saleRealPrice.add(childOrder.getSaleRealPrice() != null ? childOrder.getSaleRealPrice() : BigDecimal.ZERO);
                            }
                        }
                        checkSalePrice = checkSalePrice.add(order.getCheckCashPrice() != null ? order.getCheckCashPrice() : BigDecimal.ZERO);
                    } else {
                        if (ObjUtil.isNotNull(order.getExpectPrice()) && order.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                            if (ObjUtil.isNotNull(order.getOrderGo()) ){
                                useToPriceMap.put(order.getOrderGo(), order.getExpectPrice().add(useToPriceMap.get(order.getOrderGo())));
                            }else {
                                useToPriceMap.put(155, order.getExpectPrice().add(useToPriceMap.get(155)));
                            }
                        } else {
                            if (order.getIsBlindBox() == 2){
                                BigDecimal blindBoxOneTotalPrice = order.getTotalNum() != null ? new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(blindOnePrice)) : BigDecimal.ZERO;
                                useToPriceMap.put(5047, blindBoxOneTotalPrice.add(useToPriceMap.get(5047)));
                            }else {
                                useToPriceMap.put(155, order.getTotalPrice().add(useToPriceMap.get(155)));
                            }
                        }
                        if (order.getOrderGo() != null && order.getOrderGo() == 5047){
                            // 盲盒
                            //盲盒收入
                            if (order.getExpectPrice() != null && order.getExpectPrice().compareTo(BigDecimal.ZERO) > 0){
                                blindBoxPrice = blindBoxPrice.add(order.getExpectPrice());
                            }else {
                                if (order.getTotalNum() != null) {
                                    BigDecimal bTotalPrice = new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(blindOnePrice));
                                    blindBoxPrice = blindBoxPrice.add(bTotalPrice);
                                }
                            }
                            //盲盒实收
                            blindRealPrice = blindRealPrice.add(order.getBlindRealPrice() != null ? order.getBlindRealPrice() : BigDecimal.ZERO);
                            // 盲盒-预计成本
                            blindBoxCost = blindBoxCost.add(order.getEstimatedCost() != null ? order.getEstimatedCost() : BigDecimal.ZERO);
                            //盲盒-实际成本
                            blindBoxRealCost = blindBoxRealCost.add(order.getActualCost() != null ? order.getActualCost() : BigDecimal.ZERO);
                        }else {
                            // 其他全部算在易货表单里
                            //项目应收 = 订单去向为渠道竞价的卖货实收 + 其他的订单去向的预计变现
                            if (order.getOrderGo() != null && order.getOrderGo() == 155){
                                hasYiHuoOrder = true;
                                projectSalePrice = projectSalePrice.add(order.getSaleRealPrice() != null ? order.getSaleRealPrice() : BigDecimal.ZERO);
//                            }else {
//                                projectSalePrice = projectSalePrice.add(order.getExpectPrice() != null && order.getExpectPrice().compareTo(BigDecimal.ZERO) > 0 ? order.getExpectPrice() : BigDecimal.ZERO);
                            }
                            //卖货实收 = 订单去向为渠道竞价的卖货实收之和
                            if (order.getOrderGo() != null && order.getOrderGo() == 155){
                                projectSaleRealPrice = projectSaleRealPrice.add(order.getSaleRealPrice() != null ? order.getSaleRealPrice() : BigDecimal.ZERO);
                            }else {
                                projectOtherRealPriceFinal = projectOtherRealPriceFinal.add(order.getExpectPrice() != null && order.getExpectPrice().compareTo(BigDecimal.ZERO) > 0 ? order.getExpectPrice() : BigDecimal.ZERO);
                            }
                        }
                        // 预计成本
                        estimateCost = estimateCost.add(order.getEstimatedCost() != null ? order.getEstimatedCost() : BigDecimal.ZERO);
                        // 实际成本
                        actualCost = actualCost.add(order.getActualCost() != null ? order.getActualCost() : BigDecimal.ZERO);
                        // 开单总额/项目总货值
                        totalPrice = totalPrice.add(order.getTotalPrice() != null ? order.getTotalPrice() : BigDecimal.ZERO);
                        // 内部货值
                        innerPrice = innerPrice.add(order.getInnerPrice() != null ? order.getInnerPrice() : BigDecimal.ZERO);
                        // 项目实际货值
                        projectRealTotalPrice = projectRealTotalPrice.add(order.getRealTotalPrice() != null ? order.getRealTotalPrice() : BigDecimal.ZERO);

                        actualValuePrice = actualValuePrice.add(order.getRealTotalPrice() != null ? order.getRealTotalPrice() : BigDecimal.ZERO);
                        checkSalePrice = checkSalePrice.add(order.getCheckCashPrice() != null ? order.getCheckCashPrice() : BigDecimal.ZERO);
                        biddingSalePrice = biddingSalePrice.add(order.getBiddingTwoTopPrice() != null ? order.getBiddingTwoTopPrice() : order.getBiddingOneFirstPrice() != null ? order.getBiddingOneFirstPrice() : BigDecimal.ZERO);
                        saleRealPrice = saleRealPrice.add(order.getSaleRealPrice() != null ? order.getSaleRealPrice() : BigDecimal.ZERO);
                    }
                }

                blindBoxPriceFinal = blindBoxPriceFinal.add(blindBoxPrice);
                blindRealPriceFinal = blindRealPriceFinal.add(blindRealPrice);
                projectSalePriceFinal = projectSalePriceFinal.add(projectSalePrice);
                if(hasYiHuoOrder){
                    projectSalePriceFinal = projectSalePriceFinal.add(checkOrderPrice != null ? checkOrderPrice : BigDecimal.ZERO);
                }
                projectSaleRealPriceFinal = projectSaleRealPriceFinal.add(projectSaleRealPrice);
                estimateCostFinal = estimateCostFinal.add(estimateCost);
                actualCostFinal = actualCostFinal.add(actualCost);
                totalPriceFinal = totalPriceFinal.add(totalPrice);
                innerPriceFinal = innerPriceFinal.add(innerPrice);
                projectRealTotalPriceFinal = projectRealTotalPriceFinal.add(projectRealTotalPrice);

                //计算项目收入，根据useType进行不同的计算
                // 方案1：应收 & 预计成本
                // 方案2：实收 & 预计成本
                // 方案3：实收 & 实际成本
                // 方案4：应收/实收 & 预计成本/实际成本
                BigDecimal addPrice = BigDecimal.ZERO; // 总的应收/实收
                BigDecimal addCost = BigDecimal.ZERO; // 总的成本
                BigDecimal addBlindPrice = BigDecimal.ZERO; // 总的盲盒应收/实收
                if (useType == 1){
                    addPrice = checkOrderPrice.add(blindBoxPrice);
                    addCost = estimateCost;
                    addBlindPrice = blindBoxPrice;
                    useTypeBlindTotalCost = blindBoxCost;
                } else if (useType == 2){
                    addPrice = projectSaleRealPrice.add(blindRealPrice).add(projectOtherRealPriceFinal);
                    addCost = estimateCost;
                    addBlindPrice = blindRealPrice;
                    useTypeBlindTotalCost = blindBoxCost;
                } else if (useType == 3){
                    addPrice = projectSaleRealPrice.add(blindRealPrice).add(projectOtherRealPriceFinal);
                    addCost = actualCost;
                    addBlindPrice = blindRealPrice;
                    useTypeBlindTotalCost = blindBoxRealCost;
                } else {
                    BigDecimal a = projectSaleRealPriceFinal.compareTo(BigDecimal.ZERO) > 0 ? projectSaleRealPriceFinal : projectSalePriceFinal;
                    BigDecimal b = blindRealPriceFinal.compareTo(BigDecimal.ZERO) > 0 ? blindRealPriceFinal : blindBoxPriceFinal;
                    addPrice = a.add(b).add(projectOtherRealPriceFinal);
                    addCost = actualCost.compareTo(BigDecimal.ZERO) > 0 ? actualCost : estimateCost;
                    addBlindPrice = b;
                    useTypeBlindTotalCost = blindBoxRealCost.compareTo(BigDecimal.ZERO) > 0 ? blindBoxRealCost : blindBoxCost;
                }
                useTypeTotalPrice = useTypeTotalPrice.add(addPrice);
                useTypeTotalCost = useTypeTotalCost.add(addCost);
                useTypeBlindTotalPrice = useTypeBlindTotalPrice.add(addBlindPrice);

            }

            //客户货值
            msg.setTotalPrice(totalPriceFinal);
            //实际货值
            msg.setActualValuePrice(actualValuePrice);
            //预估成本
            msg.setEstimatedCost(estimateCostFinal);
            //实际成本
            msg.setActualCost(actualCostFinal);
            //项目成本
            msg.setProjectCost(useTypeTotalCost);
            //成本占比（项目成本/客户货值）(%)
            if (totalPriceFinal.compareTo(BigDecimal.ZERO) != 0){
                msg.setCostRate((useTypeTotalCost.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : useTypeTotalCost)
                        .divide(totalPriceFinal, 6,BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP));
            }
            //盲盒收入
            msg.setBlindBoxPrice(blindBoxPriceFinal);
            //盲盒利润： 盲盒收入-盲盒成本【按方案】
            msg.setBlindBoxRate(useTypeBlindTotalPrice.subtract(useTypeBlindTotalCost));
            //核价卖货应收
            msg.setCheckSalePrice(checkSalePrice);
            //竞价卖货应收
            msg.setBiddingSalePrice(biddingSalePrice);
            //卖货实收
            msg.setSaleRealPrice(projectSaleRealPriceFinal);
            //项目收入
            msg.setProjectPrice(useTypeTotalPrice);
            //去盲盒收入 项目收入-盲盒收入
            msg.setProjectNoBiddingPrice(useTypeTotalPrice.subtract(useTypeBlindTotalPrice));
            //去盲盒毛利 项目收入-盲盒收入-项目成本
            msg.setProjectNoBiddingRateGross(useTypeTotalPrice.subtract(useTypeBlindTotalPrice).subtract(useTypeTotalCost));
            //项目毛利:项目收入-项目成本
            msg.setProjectRate(useTypeTotalPrice.subtract(useTypeTotalCost));
            //毛利率(%)：毛利/项目收入
            if (useTypeTotalPrice.compareTo(BigDecimal.ZERO) != 0){
                BigDecimal rateGross = (
                        (useTypeTotalPrice.subtract(useTypeTotalCost)).compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : useTypeTotalPrice.subtract(useTypeTotalCost))
                        .divide(useTypeTotalPrice, 6, BigDecimal.ROUND_HALF_UP)
                        .setScale(2, BigDecimal.ROUND_HALF_UP);
                msg.setProjectRateGross(rateGross);
            }
            //出货进度:出货货值/客户货值
            if (msg.getProjectOutGoodPrice() != null){
                if (msg.getProjectOutGoodPrice().compareTo(BigDecimal.ZERO) !=0 && totalPriceFinal.compareTo(BigDecimal.ZERO) != 0){
                    BigDecimal projectOutGoodRate = ( msg.getProjectOutGoodPrice())
                            .divide(totalPriceFinal, 6, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
                    msg.setProjectOutGoodRate(projectOutGoodRate);
                }
            } else {
                msg.setProjectOutGoodRate(BigDecimal.ZERO);
            }

            //出货部分成本:预估成本*出货进度
            if (msg.getProjectOutGoodRate() != null){
                msg.setProjectOutGoodSomeRate(useTypeTotalCost.multiply(msg.getProjectOutGoodRate()).multiply(new BigDecimal(0.01)));
            } else {
                msg.setProjectOutGoodSomeRate(BigDecimal.ZERO);
            }
            //出货倍数:卖货实收/出货成本
            if (msg.getProjectOutGoodSomeRate().compareTo(BigDecimal.ZERO) != 0){
                BigDecimal projectOutGoodTimes = (saleRealPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : saleRealPrice)
                        .divide(msg.getProjectOutGoodSomeRate(), 6, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
                msg.setProjectOutGoodTimes(projectOutGoodTimes);
            } else {
                msg.setProjectOutGoodTimes(BigDecimal.ZERO);
            }

            //盲盒货值
            msg.setBlindBoxTotalPrice(blindBoxPriceFinal);

            // 用途收入map
            msg.setUseToPriceMap(useToPriceMap);
        }
        return list;
    }

    /**
     * @author: zhy
     * @description: 计算项目的部分数据
     * @create: 2023年12月01日 17:21
     * @return
     */
    public List<HomeProjectMsg> doGetProjectSomeTotalOld(List<HomeProjectMsg> list, Integer useType) {
        List<SysLabelValueListDTO> valueListDTOS = sysLabelValueMapper.selectListBySysLabelId(42, null, null);
        String blindBoxOnePrice = sysLabelValueMapper.selectValueBySysIdAndReadOne(9);
        for (HomeProjectMsg msg : list){
            Map<Integer, BigDecimal> useToPriceMap = new HashMap();
            for (SysLabelValueListDTO dto : valueListDTOS){
                useToPriceMap.put(dto.getId(), BigDecimal.ZERO);
            }
            BigDecimal totalPrice = BigDecimal.ZERO; // 客户货值
            BigDecimal actualValuePrice = BigDecimal.ZERO; // 实际货值
            BigDecimal blindBoxTotalPrice = BigDecimal.ZERO; // 盲盒货值
            BigDecimal estimatedCost = BigDecimal.ZERO; // 预估成本
            BigDecimal actualCost = BigDecimal.ZERO; // 实际成本
            BigDecimal blindBoxPrice = BigDecimal.ZERO; // 盲盒应收
            BigDecimal checkSalePrice = BigDecimal.ZERO;  // 核价卖货应收
            BigDecimal biddingSalePrice = BigDecimal.ZERO;  // 竞价卖货应收
            BigDecimal saleRealPrice = BigDecimal.ZERO;  // 卖货实收
            BigDecimal blindRealPrice = BigDecimal.ZERO; // 盲盒实收
            BigDecimal projectCost = BigDecimal.ZERO; // 项目成本
            BigDecimal heartBoxPrice = BigDecimal.ZERO; // 心动鲤盒收入
            BigDecimal ownUsePrice = BigDecimal.ZERO; // 公司自用收入
            BigDecimal otherPrice = BigDecimal.ZERO; // 其他用途收入

            List<MjOrder> orders = orderMapper.selectListByProjectId(msg.getProjectId());
            for (MjOrder order : orders){
                //先不查子订单，等查到父订单再计算
                if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                    //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                    if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                        List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                        if (CollUtil.isNotEmpty(childOrderList)){
                            for (TreeOrderListDTO childOrder : childOrderList) {
                                totalPrice = totalPrice.add(childOrder.getTotalPrice() != null ? childOrder.getTotalPrice() : BigDecimal.ZERO);
                                actualValuePrice = actualValuePrice.add(childOrder.getRealTotalPrice() != null ? childOrder.getRealTotalPrice() : BigDecimal.ZERO);
                                estimatedCost = estimatedCost.add(childOrder.getEstimatedCost() != null ? childOrder.getEstimatedCost() : BigDecimal.ZERO);
                                actualCost = actualCost.add(childOrder.getActualCost() != null ? childOrder.getActualCost() : BigDecimal.ZERO);
                                biddingSalePrice = biddingSalePrice.add(childOrder.getBiddingTwoTopPrice() != null ? childOrder.getBiddingTwoTopPrice() : childOrder.getBiddingOneFirstPrice() != null ? childOrder.getBiddingOneFirstPrice() : BigDecimal.ZERO);
                                saleRealPrice = saleRealPrice.add(childOrder.getSaleRealPrice() != null ? childOrder.getSaleRealPrice() : BigDecimal.ZERO);
                                blindRealPrice = blindRealPrice.add(childOrder.getBlindRealPrice() != null ? childOrder.getBlindRealPrice() : BigDecimal.ZERO);

                                if (ObjUtil.isNotNull(childOrder.getExpectPrice()) && childOrder.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                                    if (ObjUtil.isNotNull(childOrder.getOrderGo()) ){
                                        useToPriceMap.put(childOrder.getOrderGo(), childOrder.getExpectPrice().add(useToPriceMap.get(childOrder.getOrderGo())));
                                    }else {
                                        useToPriceMap.put(155, childOrder.getExpectPrice().add(useToPriceMap.get(155)));
                                    }
                                    //说明走的盲盒
                                    blindBoxPrice = blindBoxPrice.add(childOrder.getExpectPrice());
                                    // 盲盒货值，如果是盲盒的项目，那么盲盒货值就是客户货值，不考虑盲盒后续进行调整的情况
                                    blindBoxTotalPrice = blindBoxTotalPrice.add(childOrder.getTotalPrice() != null? childOrder.getTotalPrice() : BigDecimal.ZERO);
                                } else {
                                    if (childOrder.getIsBlindBox() == 2){
                                        BigDecimal blindBoxOneTotalPrice = childOrder.getTotalNum() != null ? new BigDecimal(childOrder.getTotalNum()).multiply(new BigDecimal(blindBoxOnePrice)) : BigDecimal.ZERO;
                                        useToPriceMap.put(5047, blindBoxOneTotalPrice.add(useToPriceMap.get(5047)));
                                        // 盲盒应收
                                        blindBoxPrice = blindBoxPrice.add(blindBoxOneTotalPrice);
                                        // 盲盒货值，如果是盲盒的项目，那么盲盒货值就是客户货值，不考虑盲盒后续进行调整的情况
                                        blindBoxTotalPrice = blindBoxTotalPrice.add(childOrder.getTotalPrice() != null? childOrder.getTotalPrice() : BigDecimal.ZERO);
                                    }else {
                                        useToPriceMap.put(155, childOrder.getTotalPrice().add(useToPriceMap.get(155)));
                                    }
                                }
                            }
                        }
                        checkSalePrice = checkSalePrice.add(order.getCheckCashPrice() != null ? order.getCheckCashPrice() : BigDecimal.ZERO);
                    } else {
                        totalPrice = totalPrice.add(order.getTotalPrice() != null ? order.getTotalPrice() : BigDecimal.ZERO);
                        actualValuePrice = actualValuePrice.add(order.getRealTotalPrice() != null ? order.getRealTotalPrice() : BigDecimal.ZERO);
                        estimatedCost = estimatedCost.add(order.getEstimatedCost() != null ? order.getEstimatedCost() : BigDecimal.ZERO);
                        actualCost = actualCost.add(order.getActualCost() != null ? order.getActualCost() : BigDecimal.ZERO);
                        checkSalePrice = checkSalePrice.add(order.getCheckCashPrice() != null ? order.getCheckCashPrice() : BigDecimal.ZERO);
                        biddingSalePrice = biddingSalePrice.add(order.getBiddingTwoTopPrice() != null ? order.getBiddingTwoTopPrice() : order.getBiddingOneFirstPrice() != null ? order.getBiddingOneFirstPrice() : BigDecimal.ZERO);
                        saleRealPrice = saleRealPrice.add(order.getSaleRealPrice() != null ? order.getSaleRealPrice() : BigDecimal.ZERO);
                        blindRealPrice = blindRealPrice.add(order.getBlindRealPrice() != null ? order.getBlindRealPrice() : BigDecimal.ZERO);

                        if (ObjUtil.isNotNull(order.getExpectPrice()) && order.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                            //说明走的盲盒
                            blindBoxPrice = blindBoxPrice.add(order.getExpectPrice());
                            // 盲盒货值，如果是盲盒的项目，那么盲盒货值就是客户货值，不考虑盲盒后续进行调整的情况
                            blindBoxTotalPrice = blindBoxTotalPrice.add(order.getTotalPrice() != null? order.getTotalPrice() : BigDecimal.ZERO);
                            if (ObjUtil.isNotNull(order.getOrderGo()) ){
                                useToPriceMap.put(order.getOrderGo(), order.getExpectPrice().add(useToPriceMap.get(order.getOrderGo())));
                            }else {
                                useToPriceMap.put(155, order.getExpectPrice().add(useToPriceMap.get(155)));
                            }
                        } else {
                            if (order.getIsBlindBox() == 2){
                                BigDecimal blindBoxOneTotalPrice = order.getTotalNum() != null ? new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(blindBoxOnePrice)) : BigDecimal.ZERO;
                                useToPriceMap.put(5047, blindBoxOneTotalPrice.add(useToPriceMap.get(5047)));
                                // 盲盒应收
                                blindBoxPrice = blindBoxPrice.add(blindBoxOneTotalPrice);
                                // 盲盒货值，如果是盲盒的项目，那么盲盒货值就是客户货值，不考虑盲盒后续进行调整的情况
                                blindBoxTotalPrice = blindBoxTotalPrice.add(order.getTotalPrice() != null? order.getTotalPrice() : BigDecimal.ZERO);
                            }else {
                                useToPriceMap.put(155, order.getTotalPrice().add(useToPriceMap.get(155)));
                            }
                        }
                    }
                }

            }
            //项目收入
            BigDecimal projectPrice = BigDecimal.ZERO;

            if (useType == 1){
                //方案一 ：项目收入 ： 核价卖货应收 + 盲盒应收
                //        项目成本 ： 预计成本
                //projectPrice = (checkSalePrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : checkSalePrice).add((estimatedCost.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : estimatedCost));
                projectPrice = (checkSalePrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : checkSalePrice.add(blindBoxPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : blindRealPrice));
                projectCost = estimatedCost.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : estimatedCost;
            }else if (useType == 2){
                //方案二 ：项目收入 ： 卖货实收  + 盲盒应收
                //        项目成本 ： 预计成本
                //projectPrice = (saleRealPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : saleRealPrice).add((estimatedCost.compareTo(BigDecimal.ZERO) == 0? BigDecimal.ZERO : estimatedCost));
                projectPrice = (saleRealPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : saleRealPrice.add(blindBoxPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : blindRealPrice));
                projectCost = estimatedCost.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : estimatedCost;

            }else if (useType == 3){
                //方案三 ：项目收入 ： 卖货实收  + 盲盒应收
                //        项目成本 ： 实际成本
                //projectPrice = (saleRealPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : saleRealPrice).add((actualCost.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : actualCost));
                projectPrice = (saleRealPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : saleRealPrice.add(blindBoxPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : blindRealPrice));
                projectCost = actualCost.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : actualCost;

            }else if (useType == 4){
                //方案四 ：项目收入：盲盒应收（预计变现/数量*9）+（卖货实收/核价应收）
                //        项目成本 ： 有实际成本用实际，没有用预计
                projectPrice = blindBoxPrice.add(saleRealPrice.compareTo(BigDecimal.ZERO) == 0 ? checkSalePrice : saleRealPrice);
                projectCost = actualCost.compareTo(BigDecimal.ZERO) == 0 ? estimatedCost: actualCost;
            }


            //客户货值
            msg.setTotalPrice(totalPrice);
            //实际货值
            msg.setActualValuePrice(actualValuePrice);
            //预估成本
            msg.setEstimatedCost(estimatedCost);
            //实际成本
            msg.setActualCost(actualCost);
            //项目成本
            msg.setProjectCost(projectCost);
            //成本占比（项目成本/客户货值）(%)
            if (totalPrice.compareTo(BigDecimal.ZERO) != 0){
                msg.setCostRate((projectCost.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : projectCost)
                        .divide(totalPrice,BigDecimal.ROUND_HALF_UP).setScale(6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP));
            }

            //盲盒收入
            msg.setBlindBoxPrice(blindRealPrice.compareTo(BigDecimal.ZERO) != 0 ? blindRealPrice : blindBoxPrice);
            //盲盒利润：盲盒应收-项目成本
            msg.setBlindBoxRate(msg.getBlindBoxPrice().subtract(projectCost).compareTo(BigDecimal.ZERO) > 0 ? msg.getBlindBoxPrice().subtract(projectCost) : BigDecimal.ZERO);
            //核价卖货应收
            msg.setCheckSalePrice(checkSalePrice);
            //竞价卖货应收
            msg.setBiddingSalePrice(biddingSalePrice);
            //卖货实收
            msg.setSaleRealPrice(saleRealPrice);
            //项目收入
            msg.setProjectPrice(projectPrice);
            //去盲盒收入 项目收入-盲盒收入
            msg.setProjectNoBiddingPrice(projectPrice.subtract(msg.getBlindBoxPrice()));
            //去盲盒毛利 项目收入-盲盒收入-项目成本
            msg.setProjectNoBiddingRateGross(projectPrice.subtract(msg.getBlindBoxPrice()).subtract(projectCost));
            //项目毛利:项目收入-项目成本
            msg.setProjectRate(projectPrice.subtract(projectCost));
            //毛利率(%)：毛利/项目收入
            if (projectPrice.compareTo(BigDecimal.ZERO) != 0){
                BigDecimal rateGross = ((projectPrice.subtract(projectCost)).compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : projectPrice.subtract(projectCost))
                        .divide(projectPrice, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                msg.setProjectRateGross(rateGross);
            }
            //出货进度:出货货值/客户货值
            if (msg.getProjectOutGoodPrice() != null){
                if (msg.getProjectOutGoodPrice().compareTo(BigDecimal.ZERO) !=0 && totalPrice.compareTo(BigDecimal.ZERO) != 0){
                    BigDecimal projectOutGoodRate = ( msg.getProjectOutGoodPrice())
                            .divide(totalPrice, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                    msg.setProjectOutGoodRate(projectOutGoodRate);
                }
            } else {
                msg.setProjectOutGoodRate(BigDecimal.ZERO);

            }

            //出货部分成本:预估成本*出货进度
            if (msg.getProjectOutGoodRate() != null){
                msg.setProjectOutGoodSomeRate(projectCost.multiply(msg.getProjectOutGoodRate()).multiply(new BigDecimal(0.01)));
            } else {
                msg.setProjectOutGoodSomeRate(BigDecimal.ZERO);
            }
            //出货倍数:卖货实收/出货成本
            if (msg.getProjectOutGoodSomeRate().compareTo(BigDecimal.ZERO) != 0){
                BigDecimal projectOutGoodTimes = (saleRealPrice.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : saleRealPrice)
                        .divide(msg.getProjectOutGoodSomeRate(), 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                msg.setProjectOutGoodTimes(projectOutGoodTimes);
            } else {
                msg.setProjectOutGoodTimes(BigDecimal.ZERO);

            }


            //盲盒货值
            msg.setBlindBoxTotalPrice(blindBoxTotalPrice);

            // 用途收入map
            msg.setUseToPriceMap(useToPriceMap);
        }
        return list;
    }

    /**
     * @author: zhy
     * @description: 获取项目的统计数据
     * @create: 2023年12月01日 17:12
     * @return
     */
    public HomeProjectTotalMsg getProjectTotalCount(List<HomeProjectMsg> list, Integer useType) {
        HomeProjectTotalMsg totalMsg = new HomeProjectTotalMsg();
        List<HomeProjectMsg> msgList = doGetProjectSomeTotal(list, useType);
        for (HomeProjectMsg msg : msgList){
            totalMsg.setInvoicePrice(totalMsg.getInvoicePrice().add(msg.getInvoicePrice() != null ? msg.getInvoicePrice() : BigDecimal.ZERO));
            totalMsg.setTotalPrice(totalMsg.getTotalPrice().add(msg.getTotalPrice()!= null? msg.getTotalPrice() : BigDecimal.ZERO));
            totalMsg.setActualValuePrice(totalMsg.getActualValuePrice().add(msg.getActualValuePrice()!= null? msg.getActualValuePrice() : BigDecimal.ZERO));
            totalMsg.setInnerPrice(totalMsg.getInnerPrice().add(msg.getInnerPrice()!= null? msg.getInnerPrice() : BigDecimal.ZERO));
            totalMsg.setCheckSalePrice(totalMsg.getCheckSalePrice().add(msg.getCheckSalePrice()!= null? msg.getCheckSalePrice() : BigDecimal.ZERO));
            totalMsg.setBiddingSalePrice(totalMsg.getBiddingSalePrice().add(msg.getBiddingSalePrice()!= null? msg.getBiddingSalePrice() : BigDecimal.ZERO));
            totalMsg.setSaleRealPrice(totalMsg.getSaleRealPrice().add(msg.getSaleRealPrice()!= null? msg.getSaleRealPrice() : BigDecimal.ZERO));
            totalMsg.setEstimatedCost(totalMsg.getEstimatedCost().add(msg.getEstimatedCost()!= null? msg.getEstimatedCost() : BigDecimal.ZERO));
            totalMsg.setActualCost(totalMsg.getActualCost().add(msg.getActualCost()!= null? msg.getActualCost() : BigDecimal.ZERO));
            totalMsg.setProjectCost(totalMsg.getProjectCost().add(msg.getProjectCost()!= null? msg.getProjectCost() : BigDecimal.ZERO));
            totalMsg.setProjectPrice(totalMsg.getProjectPrice().add(msg.getProjectPrice()!= null? msg.getProjectPrice() : BigDecimal.ZERO));
            totalMsg.setProjectNoBiddingPrice(totalMsg.getProjectNoBiddingPrice().add(msg.getProjectNoBiddingPrice()!= null? msg.getProjectNoBiddingPrice() : BigDecimal.ZERO));
            totalMsg.setProjectNoBiddingRateGross(totalMsg.getProjectNoBiddingRateGross().add(msg.getProjectNoBiddingRateGross()!= null? msg.getProjectNoBiddingRateGross() : BigDecimal.ZERO));
            totalMsg.setBlindBoxTotalPrice(totalMsg.getBlindBoxTotalPrice().add(msg.getBlindBoxTotalPrice()!= null? msg.getBlindBoxTotalPrice() : BigDecimal.ZERO));
            totalMsg.setBlindBoxPrice(totalMsg.getBlindBoxPrice().add(msg.getBlindBoxPrice()!= null? msg.getBlindBoxPrice() : BigDecimal.ZERO));
            totalMsg.setEstimatedSelfMediaCost(totalMsg.getEstimatedSelfMediaCost().add(msg.getEstimatedSelfMediaCost()!= null? msg.getEstimatedSelfMediaCost() : BigDecimal.ZERO));
            totalMsg.setEstimatedExternalMediaCost(totalMsg.getEstimatedExternalMediaCost().add(msg.getEstimatedExternalMediaCost()!= null? msg.getEstimatedExternalMediaCost() : BigDecimal.ZERO));
            totalMsg.setActualSelfMediaCost(totalMsg.getActualSelfMediaCost().add(msg.getActualSelfMediaCost()!= null? msg.getActualSelfMediaCost() : BigDecimal.ZERO));
            totalMsg.setActualExternalMediaCost(totalMsg.getActualExternalMediaCost().add(msg.getActualExternalMediaCost()!= null? msg.getActualExternalMediaCost() : BigDecimal.ZERO));
        }
        List<BigDecimal> innerPriceList = new ArrayList<>();
        List<BigDecimal> actualValuePriceList = new ArrayList<>();
        List<BigDecimal> saleRealPriceList = new ArrayList<>();
        List<BigDecimal> estimatedCostList = new ArrayList<>();
        List<BigDecimal> actualCostList = new ArrayList<>();
        List<BigDecimal> estimatedSelfMediaCostList = new ArrayList<>();
        List<BigDecimal> estimatedExternalMediaCostList = new ArrayList<>();
        List<BigDecimal> actualSelfMediaCostList = new ArrayList<>();
        List<BigDecimal> actualExternalMediaCostList = new ArrayList<>();
        List<BigDecimal> arrivalRateList = new ArrayList<>();
        for (HomeProjectMsg msg : list){
            // 判断列表中是否包含各自的参数
            if (!innerPriceList.contains(msg.getInnerPrice())){
                innerPriceList.add(msg.getInnerPrice());
            }
            if (!actualValuePriceList.contains(msg.getActualValuePrice())){
                actualValuePriceList.add(msg.getActualValuePrice());
            }
            if (!saleRealPriceList.contains(msg.getSaleRealPrice())){
                saleRealPriceList.add(msg.getSaleRealPrice());
            }
            if (!estimatedCostList.contains(msg.getEstimatedCost())){
                estimatedCostList.add(msg.getEstimatedCost());
            }
            if (!actualCostList.contains(msg.getActualCost())){
                actualCostList.add(msg.getActualCost());
            }
            if (!arrivalRateList.contains(msg.getArrivalRate())){
                arrivalRateList.add(msg.getArrivalRate());
            }
            if (!estimatedSelfMediaCostList.contains(msg.getEstimatedSelfMediaCost())){
                estimatedSelfMediaCostList.add(msg.getEstimatedSelfMediaCost());
            }
            if (!estimatedExternalMediaCostList.contains(msg.getEstimatedExternalMediaCost())){
                estimatedExternalMediaCostList.add(msg.getEstimatedExternalMediaCost());
            }
            if (!actualSelfMediaCostList.contains(msg.getActualSelfMediaCost())){
                actualSelfMediaCostList.add(msg.getActualSelfMediaCost());
            }
            if (!actualExternalMediaCostList.contains(msg.getActualExternalMediaCost())){
                actualExternalMediaCostList.add(msg.getActualExternalMediaCost());
            }
        }
        totalMsg.setInnerPriceList(innerPriceList);
        totalMsg.setActualValuePriceList(actualValuePriceList);
        totalMsg.setSaleRealPriceList(saleRealPriceList);
        totalMsg.setEstimatedCostList(estimatedCostList);
        totalMsg.setActualCostList(actualCostList);
        totalMsg.setEstimatedSelfMediaCostList(estimatedSelfMediaCostList);
        totalMsg.setEstimatedExternalMediaCostList(estimatedExternalMediaCostList);
        totalMsg.setActualSelfMediaCostList(actualSelfMediaCostList);
        totalMsg.setActualExternalMediaCostList(actualExternalMediaCostList);
        totalMsg.setArrivalRateList(arrivalRateList);
        List<SysLabelValueListDTO> valueListDTOS = sysLabelValueMapper.selectListBySysLabelId(42, null, null);
        Map<Integer, String> useToPriceMap = new HashMap();
        for (SysLabelValueListDTO dto : valueListDTOS){
            useToPriceMap.put(dto.getId(), dto.getSysValueName());
        }
        totalMsg.setUseToPriceStringMap(useToPriceMap);
        return totalMsg;
    }



    public APPResultMessage getBusinessMonthList2(Date time) {
        Map<Integer, HomeBusinessList2DTO> map = new HashMap();
        List<SysLabelValueListDTO> sysLabelValueListDTOS = sysLabelValueMapper.selectAllArea();
        for (SysLabelValueListDTO sysLabelDto : sysLabelValueListDTOS) {
            map.put(sysLabelDto.getId(), new HomeBusinessList2DTO(sysLabelDto.getSysValueName(), sysLabelDto.getId()));
        }
        for (Integer areaId : map.keySet()) {
            // 各个区域已签单商务总人数
            int hasOrderNum = projectMapper.selectCountByTimeAndAreaId(time, areaId);
            map.get(areaId).setHasOrderBusinessNum(hasOrderNum);
            // 各个区域商务总人数
            List<MjBusinessPerson> totalNumList = businessPersonMapper.selectCountByTimeAndAreaId(time, areaId);
            map.get(areaId).setBusinessTotalNum(totalNumList.size());
            //各个区域已签单项目数
            Integer hasCheckNum = projectMapper.selectProjectCountByTimeAndAreaId(time, areaId);
            //各个区域总订单数（不计算子订单）
            Integer mjOrders = orderMapper.selectCountOrder(time, areaId);
            if ( mjOrders > 0) {
                double ratio = ((double) hasCheckNum / mjOrders) * 100;
                DecimalFormat df = new DecimalFormat("#.##");
                String formattedRatio = df.format(ratio);
                map.get(areaId).setHasCheckRate(formattedRatio);
            } else {
                map.get(areaId).setHasCheckRate("0");
            }

        }
        //总货值
        List<MjProject> projects = projectMapper.selectHomeListByTime(time);
        for (MjProject project : projects) {
            List<MjOrder> orders = orderMapper.selectListByProjectId(project.getId());
            for (MjOrder order : orders) {
                //先不找子订单，等查到父订单再计算
                if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                    //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                    if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                        List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                        if (CollUtil.isNotEmpty(childOrderList)){
                            for (TreeOrderListDTO childOrder : childOrderList) {
                                for (Integer areaId : map.keySet()) {
                                    if (areaId != null && areaId == Integer.parseInt(project.getAreaId())) {
                                        HomeBusinessList2DTO businessObj = map.get(areaId);
                                        businessObj.setOrderTotalPrice(businessObj.getOrderTotalPrice() != null ?
                                                (businessObj.getOrderTotalPrice().add(childOrder.getTotalPrice() == null ? BigDecimal.ZERO : childOrder.getTotalPrice())) : BigDecimal.ZERO);
                                    }
                                }
                            }
                        }
                    } else {
                        for (Integer areaId : map.keySet()) {
                            if (areaId != null && areaId == Integer.parseInt(project.getAreaId())) {
                                HomeBusinessList2DTO businessObj = map.get(areaId);
                                businessObj.setOrderTotalPrice(businessObj.getOrderTotalPrice() != null ?
                                        (businessObj.getOrderTotalPrice().add(order.getTotalPrice() == null ? BigDecimal.ZERO : order.getTotalPrice())) : BigDecimal.ZERO);
                            }
                        }
                    }
                }

            }
        }
        List<HomeBusinessListDTO> list = businessPersonMapper.selectHomeListByTime(time, null, null, null);
        String blindOnePrice = sysLabelValueMapper.selectValueBySysIdAndReadOne(9);
        for (HomeBusinessListDTO dto1 : list) {
            // 底薪
            BigDecimal basicSalary = BigDecimal.ZERO;
            // 绩效
            BigDecimal performanceGoodsPrice = BigDecimal.ZERO;
            MjBusinessTimePrice businessTimePrice = businessTimePriceMapper.selectMsgByBusinessIdAndTime(dto1.getBusinessId(), time);
            if (businessTimePrice != null) {
                basicSalary = businessTimePrice.getBasicSalary() != null ? businessTimePrice.getBasicSalary() : BigDecimal.ZERO;
                performanceGoodsPrice = businessTimePrice.getPerformanceGoodsPrice() != null ? businessTimePrice.getPerformanceGoodsPrice() : BigDecimal.ZERO;
            }
            dto1.setBasicSalary(basicSalary);
            // 工资
            BigDecimal payrollPrice = payrollMapper.selectPayPriceByUsername(dto1.getBusinessName(), time, time);
            dto1.setPayRollPrice(payrollPrice);

            List<MjProject> mjProjects = projectMapper.selectHomeListByBusinessIdAndTime(dto1.getBusinessId(), time, time);
            BusinessLevelDTO levelDTO = businessManageService.getBusinessOrder(dto1.getBusinessId(), mjProjects, payrollPrice,null, 4, blindOnePrice);
            // 毛利
            BigDecimal projectRate = levelDTO.getExpectGrossProfit();
            dto1.setProjectRate(projectRate);
            // 去工资后利润：毛利-工资
            dto1.setNoPayRollRate(levelDTO.getNoPayPriceProfit());

            // 内部货值
            dto1.setInnerPrice(levelDTO.getInnerPrice());

            //内部货值和去资利润
            for (Integer areaId : map.keySet()) {
                if (areaId != null && areaId.equals(dto1.getAreaId())) {
                    HomeBusinessList2DTO mapDto2 = map.get(areaId);
                    mapDto2.setInnerPrice(mapDto2.getInnerPrice().add(dto1.getInnerPrice()));
                    mapDto2.setNoPayRollRate(mapDto2.getNoPayRollRate().add(dto1.getNoPayRollRate()));
                }
            }

        }

        List<HomeBusinessList2DTO> valuesList = new ArrayList<>(map.values());
        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(valuesList);
            successMessage.setResult(resultMessage);
            return successMessage;
    }


    //获得每个月的统计数
    public APPResultMessage getChildrenList(Date time, Integer areaId) {
        List<HomeBusinessList2DTO> groupList = new ArrayList<>();
        //该区域下的所有小组
        List<SysLabelValueListDTO> labelGroupList = sysLabelValueMapper.selectAllGroup(areaId);
        for (SysLabelValueListDTO group : labelGroupList) {
            HomeBusinessList2DTO groupObj = new HomeBusinessList2DTO();
            groupObj.setArea(group.getSysValueName());
            groupObj.setAreaId(group.getId());
            //各个小组签单人数
            int hasCheckBusinessNum = projectMapper.selectCheckGroup(time, group.getId());
            groupObj.setHasOrderBusinessNum(hasCheckBusinessNum);
            //各个小组总人数
            int groupNum = businessPersonMapper.selectCountGroup(time, group.getId());
            groupObj.setBusinessTotalNum(groupNum);
            //各个小组已签单项目数
            int hasCheckNum = projectMapper.selectProjectCountByGroupId(time, group.getId());
            //各个小组总订单数
            int groupOrderNum = orderMapper.selectGroupCountOrder(time, group.getId());
            if ( groupOrderNum > 0) {
                double ratio = ((double) hasCheckNum / groupOrderNum) * 100;
                DecimalFormat df = new DecimalFormat("#.##");
                String formattedRatio = df.format(ratio);
                groupObj.setHasCheckRate(formattedRatio);
            } else {
                groupObj.setHasCheckRate("0");
            }
            //每个组的总货值
            List<MjProject> projects = projectMapper.selectGroupHomeListByTime(time, group.getId());
            for (MjProject project : projects) {
                List<MjOrder> orders = orderMapper.selectListByProjectId(project.getId());
                for (MjOrder order : orders) {
                    //先不找子订单，等查到父订单再计算
                    if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                        //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                        if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                            List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                            if (CollUtil.isNotEmpty(childOrderList)){
                                for (TreeOrderListDTO childOrder : childOrderList) {
                                    groupObj.setOrderTotalPrice(groupObj.getOrderTotalPrice() != null ?
                                            (groupObj.getOrderTotalPrice().add(childOrder.getTotalPrice() == null ? BigDecimal.ZERO : childOrder.getTotalPrice())) : BigDecimal.ZERO);
                                }
                            }
                        } else {
                            groupObj.setOrderTotalPrice(groupObj.getOrderTotalPrice() != null ?
                                    (groupObj.getOrderTotalPrice().add(order.getTotalPrice() == null ? BigDecimal.ZERO : order.getTotalPrice())) : BigDecimal.ZERO);
                        }
                    }

                }
            }

            String blindOnePrice = sysLabelValueMapper.selectValueBySysIdAndReadOne(9);
            List<HomeBusinessListDTO> list = businessPersonMapper.selectHomeListByTime(time, null, null, group.getId());
            for (HomeBusinessListDTO dto1 : list) {
                // 底薪
                BigDecimal basicSalary = BigDecimal.ZERO;
                // 绩效
                BigDecimal performanceGoodsPrice = BigDecimal.ZERO;
                MjBusinessTimePrice businessTimePrice = businessTimePriceMapper.selectMsgByBusinessIdAndTime(dto1.getBusinessId(), time);
                if (businessTimePrice != null) {
                    basicSalary = businessTimePrice.getBasicSalary() != null ? businessTimePrice.getBasicSalary() : BigDecimal.ZERO;
                    performanceGoodsPrice = businessTimePrice.getPerformanceGoodsPrice() != null ? businessTimePrice.getPerformanceGoodsPrice() : BigDecimal.ZERO;
                }
                dto1.setBasicSalary(basicSalary);
                // 工资
                BigDecimal payrollPrice = payrollMapper.selectPayPriceByUsername(dto1.getBusinessName(), time, time);
                dto1.setPayRollPrice(payrollPrice);

                List<MjProject> mjProjects = projectMapper.selectHomeListByBusinessIdAndTime(dto1.getBusinessId(), time, time);
                BusinessLevelDTO levelDTO = businessManageService.getBusinessOrder(dto1.getBusinessId(), mjProjects, payrollPrice,null,
                        4, blindOnePrice);
                // 毛利
                BigDecimal projectRate = levelDTO.getExpectGrossProfit();
                dto1.setProjectRate(projectRate);
                // 去工资后利润：毛利-工资
                dto1.setNoPayRollRate(levelDTO.getNoPayPriceProfit());

                // 内部货值
                dto1.setInnerPrice(levelDTO.getInnerPrice());

                groupObj.setInnerPrice(groupObj.getInnerPrice().add(dto1.getInnerPrice()));
                groupObj.setNoPayRollRate(groupObj.getNoPayRollRate().add(dto1.getNoPayRollRate()));
            }

            groupList.add(groupObj);
        }

        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setList(groupList);
        successMessage.setResult(resultMessage);
        return successMessage;

    }






    // 获取评级
    public BigDecimal getRateLevel(Integer businessId, String businessUsername, Date time){
        BigDecimal payPrice = payrollMapper.selectPayPriceByUsername(businessUsername, time, time);
        List<MjProject> mjProjects = projectMapper.selectHomeListByBusinessIdAndTime(businessId, time, time);
        String blindOnePrice = sysLabelValueMapper.selectValueBySysIdAndReadOne(9);
        BusinessLevelDTO levelDTO = businessManageService.getBusinessOrder(businessId, mjProjects, payPrice == null ? BigDecimal.ZERO : payPrice,
                null, 4, blindOnePrice);
        BigDecimal rateLevel = levelDTO.getRateLevel();
        return rateLevel;
    }

    /**
     * @author: ljl
     * @description: 商务个人利润
     * @create: 2024年03月03日 13:18
     * @return
     */
    public APPResultMessage getTotalPersonProfit(Date time, Integer pageNum, Integer pageSize, String businessIds, String area,Integer blindType) {
        //年度利润
        BigDecimal totalYearPrice = BigDecimal.ZERO;
        //年度去盲盒利润
        BigDecimal totalYearPrice2 = BigDecimal.ZERO;
        String[] businessArr = null;
        if (StringUtil.isNotBlank(businessIds)){
            businessArr = businessIds.split(",");
        }
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
            //得到有时间限制的商务列表
            List<TotalPersonListDTO> list = businessPersonMapper.selectPersonListByTime(time, businessArr, area);
            PageInfo<TotalPersonListDTO> tables = new PageInfo<>(list);
            //计算十二个月的利润
            getMonthProfitAsync(time, tables.getList(), totalYearPrice, totalYearPrice2,blindType);
            //计算个人所有年的总利润
            List<TotalPersonListDTO> listNoTime = getAllYearProfitAsync(tables.getList(),blindType);
            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(listNoTime).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            successMessage.setResult(resultMessage);
            return successMessage;
        } else {
            List<TotalPersonListDTO> list = businessPersonMapper.selectPersonListByTime(time, businessArr, area);
            //计算十二个月的利润
            getMonthProfitAsync(time,list, totalYearPrice, totalYearPrice2,blindType);
            //计算个人所有年的总利润
            List<TotalPersonListDTO> listNoTime = getAllYearProfitAsync(list,blindType);
            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(listNoTime);
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }



    public List<TotalPersonListDTO> getMonthProfitAsync(Date time, List<TotalPersonListDTO> list, BigDecimal totalYearPrice, BigDecimal totalYearPrice2, Integer blindType) {
        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

        // 用于存储每个员工Future的结果
        List<Future<TotalPersonListDTO>> personFutures = new ArrayList<>();

        for (TotalPersonListDTO personListDTO : list) {
            Future<TotalPersonListDTO> future = executor.submit(() -> {
                TotalPersonListDTO result = personListDTO;

                for (int i = 1; i <= 12; i++) {
                    BigDecimal monthProjectCost = BigDecimal.ZERO;
                    BigDecimal monthProjectPrice = BigDecimal.ZERO;
                    BigDecimal monthBlindPrice = BigDecimal.ZERO;
                    BigDecimal personMonthSalary = BigDecimal.ZERO;

                    personMonthSalary = payrollMapper.selectPayPriceByUsername2(personListDTO.getName(), time, i);
                    List<MjProject> mjProjects = projectMapper.selectHomeListByBusinessIdAndTime2(personListDTO.getBusinessId(), time, i);

                    for (MjProject mjProject : mjProjects) {
                        List<MjOrder> orders = orderMapper.selectListByProjectId(mjProject.getId());
                        for (MjOrder order : orders) {
                            //先不找子订单，等查到父订单再计算
                            if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                                //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                                if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                                    //子订单卖货实收
                                    BigDecimal orderSealRealPrice = BigDecimal.ZERO;
                                    //子订单盲盒收入
                                    BigDecimal orderBlindBoxPrice = BigDecimal.ZERO;
                                    List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                                    if (CollUtil.isNotEmpty(childOrderList)){
                                        for (TreeOrderListDTO childOrder : childOrderList) {
                                            //子订单盲盒收入
                                            if (ObjUtil.isNotNull(childOrder.getExpectPrice()) && childOrder.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                                                //说明走的盲盒
                                                orderBlindBoxPrice = orderBlindBoxPrice.add(childOrder.getExpectPrice());
                                            } else if (childOrder.getTotalNum() != null && childOrder.getIsBlindBox() == 2){

                                                orderBlindBoxPrice = orderBlindBoxPrice.add(new BigDecimal(childOrder.getTotalNum()).multiply(new BigDecimal(9)));
                                            } else {
                                                //子订单卖货实收
                                                orderSealRealPrice = orderSealRealPrice.add(childOrder.getSaleRealPrice() != null ? childOrder.getSaleRealPrice() : BigDecimal.ZERO);
                                            }
                                            // 项目成本 ： 有实际成本用实际，没有用预计
                                            if (order.getActualCost() != null && order.getActualCost().compareTo(BigDecimal.ZERO) != 0){
                                                monthProjectCost = monthProjectCost.add(order.getActualCost());
                                            } else if (order.getEstimatedCost() != null){
                                                monthProjectCost = monthProjectCost.add(order.getEstimatedCost());
                                            }
                                        }
                                    }
                                    //项目收入
                                    monthProjectPrice = monthProjectPrice.add(orderBlindBoxPrice).add(orderSealRealPrice.compareTo(BigDecimal.ZERO) == 0 ? (order.getCheckCashPrice() != null ? order.getCheckCashPrice() : BigDecimal.ZERO) : orderSealRealPrice);
                                    //盲盒应收
                                    monthBlindPrice = monthBlindPrice.add(orderBlindBoxPrice);
                                } else {
                                    //盲盒收入（应收）
                                    if (ObjUtil.isNotNull(order.getExpectPrice()) && order.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                                        monthProjectPrice = monthProjectPrice.add(order.getExpectPrice());
                                        monthBlindPrice = monthBlindPrice.add(order.getExpectPrice());
                                    } else if (order.getTotalNum() != null && order.getIsBlindBox() == 2){
                                        monthProjectPrice = monthProjectPrice.add(new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(9)));
                                        monthBlindPrice = monthBlindPrice.add(new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(9)));
                                    } else {
                                        //卖货收入
                                        if (order.getSaleRealPrice() != null && order.getSaleRealPrice().compareTo(BigDecimal.ZERO) != 0){
                                            //卖货实收
                                            monthProjectPrice = monthProjectPrice.add(order.getSaleRealPrice());
                                        } else {
                                            // 卖货应收
                                            monthProjectPrice = monthProjectPrice.add(order.getCheckCashPrice() != null ? order.getCheckCashPrice() : BigDecimal.ZERO);
                                        }
                                    }
                                    // 项目成本 ： 有实际成本用实际，没有用预计
                                    if (order.getActualCost() != null && order.getActualCost().compareTo(BigDecimal.ZERO) != 0){
                                        monthProjectCost = monthProjectCost.add(order.getActualCost());
                                    } else if (order.getEstimatedCost() != null){
                                        monthProjectCost = monthProjectCost.add(order.getEstimatedCost());
                                    }
                                }
                            }

                        }
                    }

                    // 调用getMonth方法计算本月利润，并更新dto对象
                    result = getMonth(monthProjectCost, monthProjectPrice, monthBlindPrice, i, result, personMonthSalary, blindType);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                    String format = sdf.format(time);
                    if (format.equals("2023")) {
                        Map<String,PersonPreProfit> map = initMap();
                        PersonPreProfit personPreProfit = map.get(result.getName());
                        if (blindType == 1){
                            if (personPreProfit != null){
                                result.setSeptemberPersonProfit(personPreProfit.getProfit());
                            }
                        } else {
                            if (personPreProfit != null){
                                result.setSeptemberPersonProfit(personPreProfit.getNoBlindProfit());
                            }
                        }
                    }

                }
                // 更新年度累计利润
                calculateYearlyProfit(result, totalYearPrice);
                return result;
            });
            personFutures.add(future);
        }

        // 等待所有任务完成并收集结果
        List<TotalPersonListDTO> processedList = new ArrayList<>();
        for (Future<TotalPersonListDTO> future : personFutures) {
            try {
                processedList.add(future.get());
            } catch (InterruptedException | ExecutionException e) {
                // 处理异常，如记录日志或重新抛出异常
                e.printStackTrace();
            }
        }

        // 关闭线程池
        executor.shutdown();

        return processedList;
    }

    // 更新单个员工的年度总利润
    private void calculateYearlyProfit(TotalPersonListDTO personListDTO, BigDecimal totalYearPrice) {
        totalYearPrice = totalYearPrice.add(personListDTO.getJanuaryPersonProfit())
                .add(personListDTO.getFebruaryPersonProfit())
                .add(personListDTO.getMarchPersonProfit())
                .add(personListDTO.getAprilPersonProfit())
                .add(personListDTO.getMayPersonProfit())
                .add(personListDTO.getJunePersonProfit())
                .add(personListDTO.getJulyPersonProfit())
                .add(personListDTO.getAugustPersonProfit())
                .add(personListDTO.getSeptemberPersonProfit())
                .add(personListDTO.getOctoberPersonProfit())
                .add(personListDTO.getNovemberPersonProfit())
                .add(personListDTO.getDecemberPersonProfit());
        personListDTO.setYearTotalPersonProfit(totalYearPrice);
    }

    public List<TotalPersonListDTO> getAllYearProfitAsync(List<TotalPersonListDTO> list, Integer blindType) {
        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

        // 使用 CompletableFuture 进行异步处理
        List<CompletableFuture<TotalPersonListDTO>> futures = list.stream()
                .map(personListDTO -> CompletableFuture.supplyAsync(() -> getAllYearProfit(personListDTO, blindType), executor))
                .collect(Collectors.toList());

        // 等待所有任务完成并收集结果
        List<TotalPersonListDTO> result = futures.stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList());

        // 关闭线程池
        executor.shutdown();

        return result;
    }
        //得到所有年的累计利润
        public TotalPersonListDTO getAllYearProfit(TotalPersonListDTO personListDTO, Integer blindType) {
            BigDecimal personTotalSalary = BigDecimal.ZERO;
            BigDecimal projectTotalPrice = BigDecimal.ZERO;
            BigDecimal projectTotalCost = BigDecimal.ZERO;
            BigDecimal personTotalPrice = BigDecimal.ZERO;
            BigDecimal personTotalNoBlind = BigDecimal.ZERO;
            BigDecimal personTotalBlind = BigDecimal.ZERO;
                //商务总工资
                personTotalSalary = payrollMapper.selectPayPriceByUsername(personListDTO.getName(), null, null);
                //商务总订单

                List<MjProject> mjProjects = projectMapper.selectHomeListByBusinessIdAndTime(personListDTO.getBusinessId(), null, null);
                for (MjProject mjProject : mjProjects) {

                    List<MjOrder> orders = orderMapper.selectListByProjectId(mjProject.getId());

                    for (MjOrder order : orders) {
                        //先不找子订单，等查到父订单再计算
                        if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                            //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                            if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                                //子订单卖货实收
                                BigDecimal orderSealRealPrice = BigDecimal.ZERO;
                                List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                                if (CollUtil.isNotEmpty(childOrderList)){
                                    for (TreeOrderListDTO childOrder : childOrderList) {
                                        //方案四 ：
                                        //子订单卖货实收
                                        if (childOrder.getSaleRealPrice() != null && childOrder.getSaleRealPrice().compareTo(BigDecimal.ZERO) != 0){
                                            if (ObjUtil.isNotNull(childOrder.getExpectPrice()) && childOrder.getExpectPrice().compareTo(BigDecimal.ZERO) != 0){
                                                //说明走的盲盒
                                                orderSealRealPrice = orderSealRealPrice.add(childOrder.getExpectPrice());
                                            } else {
                                                if (childOrder.getTotalNum() != null && childOrder.getIsBlindBox() == 2){
                                                    BigDecimal a = new BigDecimal(childOrder.getTotalNum()).multiply(new BigDecimal(9));
                                                    orderSealRealPrice = orderSealRealPrice.add(a);
                                                }
                                            }
                                            orderSealRealPrice = orderSealRealPrice.add(childOrder.getSaleRealPrice());
                                        }

                                        // 项目成本 ： 有实际成本用实际，没有用预计
                                        if (order.getActualCost() != null && order.getActualCost().compareTo(BigDecimal.ZERO) != 0){
                                            projectTotalCost = projectTotalCost.add(order.getActualCost());
                                        } else if (order.getEstimatedCost() != null){
                                            projectTotalCost = projectTotalCost.add(order.getEstimatedCost());
                                        }

                                        // 盲盒收入
                                        if (order.getTotalNum() != null && order.getIsBlindBox() == 2){
                                            personTotalBlind = personTotalBlind.add(new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(9)));
                                        }
                                    }
                                }
                                if (orderSealRealPrice.compareTo(BigDecimal.ZERO) != 0){
                                    projectTotalPrice = projectTotalPrice.add(orderSealRealPrice);
                                } else {
                                    //核价卖货应收
                                    if (order.getCheckCashPrice() != null){
                                        projectTotalPrice = projectTotalPrice.add(order.getCheckCashPrice());
                                    }
                                }
                            } else {
                                //方案四 ：
                                // 项目收入 ： 有（卖货实收  + 盲盒应收）实收，没有就核价卖货应收
                                if (order.getSaleRealPrice() != null && order.getSaleRealPrice().compareTo(BigDecimal.ZERO) != 0){
                                    if (order.getTotalNum() != null && order.getIsBlindBox() == 2){
                                        projectTotalPrice = projectTotalPrice.add(new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(9)));
                                    }
                                    projectTotalPrice = projectTotalPrice.add(order.getSaleRealPrice());
                                } else if (order.getCheckCashPrice() != null){
                                    projectTotalPrice = projectTotalPrice.add(order.getCheckCashPrice());
                                }

                                // 项目成本 ： 有实际成本用实际，没有用预计
                                if (order.getActualCost() != null && order.getActualCost().compareTo(BigDecimal.ZERO) != 0){
                                    projectTotalCost = projectTotalCost.add(order.getActualCost());
                                } else if (order.getEstimatedCost() != null){
                                    projectTotalCost = projectTotalCost.add(order.getEstimatedCost());
                                }

                                // 盲盒收入
                                if (order.getTotalNum() != null && order.getIsBlindBox() == 2){
                                    personTotalBlind = personTotalBlind.add(new BigDecimal(order.getTotalNum()).multiply(new BigDecimal(9)));
                                }
                            }
                        }
                    }
                    if (personTotalSalary != null){
                        //累计利润  :收入-成本-薪资
                        personTotalPrice = projectTotalPrice.subtract(projectTotalCost).subtract(personTotalSalary);
                        //月累计去盲盒利润 项目收入-项目成本-工资-盲盒收入
                        personTotalNoBlind = projectTotalPrice.subtract(projectTotalCost).subtract(personTotalSalary).subtract(personTotalBlind);
                    } else {
                        personTotalPrice = projectTotalPrice.subtract(projectTotalCost);
                        personTotalNoBlind = projectTotalPrice.subtract(projectTotalCost).subtract(personTotalBlind);
                    }
                }


                Map<String,PersonPreProfit> map = initMap();
                PersonPreProfit personPreProfit = map.get(personListDTO.getName());
                if (blindType == 1){
                    personListDTO.setTotalPersonProfit(personTotalPrice);
                    if (personPreProfit != null){
                        personListDTO.setTotalPersonProfit(personListDTO.getTotalPersonProfit().add(personPreProfit.getProfit()));
                    }
                } else {
                    personListDTO.setTotalPersonProfit(personTotalNoBlind);
                    if (personPreProfit != null){
                        personListDTO.setTotalPersonProfit(personListDTO.getTotalPersonProfit().add(personPreProfit.getNoBlindProfit()));
                    }
                }
            return personListDTO;
        }


            /*判断月份*/
        public TotalPersonListDTO getMonth(BigDecimal monthProjectCost,BigDecimal monthProjectPrice,BigDecimal monthBlindPrice
                ,Integer month,TotalPersonListDTO dto,BigDecimal personMonthSalary,Integer blindType) {
            //月累计利润 项目收入-项目成本-工资
            BigDecimal totalMonthPrice = BigDecimal.ZERO;
            //月累计去盲盒利润 项目收入-项目成本-工资-盲盒收入
            BigDecimal totalMonthPrice2 = BigDecimal.ZERO;


            if (personMonthSalary != null){
                totalMonthPrice = monthProjectPrice.subtract(monthProjectCost).subtract(personMonthSalary);
                totalMonthPrice2 = monthProjectPrice.subtract(monthProjectCost).subtract(monthBlindPrice).subtract(personMonthSalary);
            } else {
                totalMonthPrice = monthProjectPrice.subtract(monthProjectCost);
                totalMonthPrice2 = monthProjectPrice.subtract(monthProjectCost).subtract(monthBlindPrice);
            }
            //累计利润
            if (blindType == 1){
                if (month == 1){
                    dto.setJanuaryPersonProfit(totalMonthPrice);
                } else if (month == 2){
                    dto.setFebruaryPersonProfit(totalMonthPrice);
                } else if (month == 3){
                    dto.setMarchPersonProfit(totalMonthPrice);
                } else if (month == 4){
                    dto.setAprilPersonProfit(totalMonthPrice);
                } else if (month == 5){
                    dto.setMayPersonProfit(totalMonthPrice);
                } else if (month == 6){
                    dto.setJunePersonProfit(totalMonthPrice);
                } else if (month == 7){
                    dto.setJulyPersonProfit(totalMonthPrice);
                } else if (month == 8){
                    dto.setAugustPersonProfit(totalMonthPrice);
                } else if (month == 9){
                    dto.setSeptemberPersonProfit(totalMonthPrice);
                } else if (month == 10){
                    dto.setOctoberPersonProfit(totalMonthPrice);
                } else if (month == 11) {
                    dto.setNovemberPersonProfit(totalMonthPrice);
                } else {
                    dto.setDecemberPersonProfit(totalMonthPrice);
                }
            } else {
                if (month == 1){
                    dto.setJanuaryPersonProfit(totalMonthPrice2);
                } else if (month == 2){
                    dto.setFebruaryPersonProfit(totalMonthPrice2);
                } else if (month == 3){
                    dto.setMarchPersonProfit(totalMonthPrice2);
                } else if (month == 4){
                    dto.setAprilPersonProfit(totalMonthPrice2);
                } else if (month == 5){
                    dto.setMayPersonProfit(totalMonthPrice2);
                } else if (month == 6){
                    dto.setJunePersonProfit(totalMonthPrice2);
                } else if (month == 7){
                    dto.setJulyPersonProfit(totalMonthPrice2);
                } else if (month == 8){
                    dto.setAugustPersonProfit(totalMonthPrice2);
                } else if (month == 9){
                    dto.setSeptemberPersonProfit(totalMonthPrice2);
                } else if (month == 10){
                    dto.setOctoberPersonProfit(totalMonthPrice2);
                } else if (month == 11) {
                    dto.setNovemberPersonProfit(totalMonthPrice2);
                } else {
                    dto.setDecemberPersonProfit(totalMonthPrice2);
                }
            }
            return dto;
        }
        public APPResultMessage getBusinessHomeMonthList(Date time, Integer pageNum, Integer pageSize, String businessIds, String area, Integer targetId) {
        String[] businessArr = null;
        if (StringUtil.isNotBlank(businessIds)){
            businessArr = businessIds.split(",");
        }
        String blindOnePrice = sysLabelValueMapper.selectValueBySysIdAndReadOne(9);
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<HomeBusinessListDTO> list = businessPersonMapper.selectHomeListByTime(time, businessArr, area,targetId);
            PageInfo<HomeBusinessListDTO> tables = new PageInfo<>(list);
            for (HomeBusinessListDTO dto : tables.getList()){
                // 底薪
                BigDecimal basicSalary = BigDecimal.ZERO;
                // 绩效
                BigDecimal performanceGoodsPrice = BigDecimal.ZERO;
                MjBusinessTimePrice businessTimePrice = businessTimePriceMapper.selectMsgByBusinessIdAndTime(dto.getBusinessId(), time);
                if (businessTimePrice != null){
                    basicSalary = businessTimePrice.getBasicSalary() != null ? businessTimePrice.getBasicSalary() : BigDecimal.ZERO;
                    performanceGoodsPrice = businessTimePrice.getPerformanceGoodsPrice() != null ? businessTimePrice.getPerformanceGoodsPrice() : BigDecimal.ZERO;
                }
                dto.setBasicSalary(basicSalary);
                // 工资
                BigDecimal payrollPrice = payrollMapper.selectPayPriceByUsername(dto.getBusinessName(), time, time);
                dto.setPayRollPrice(payrollPrice);

                List<MjProject> mjProjects = projectMapper.selectHomeListByBusinessIdAndTime(dto.getBusinessId(), time, time);
                BusinessLevelDTO levelDTO = businessManageService.getBusinessOrder(dto.getBusinessId(), mjProjects, payrollPrice,time, 4,
                        blindOnePrice);
                // 毛利
                BigDecimal projectRate = levelDTO.getExpectGrossProfit();
                dto.setProjectRate(projectRate);
                // 去工资后利润：毛利-工资
                dto.setNoPayRollRate(levelDTO.getNoPayPriceProfit());
                // 利润倍数：去工资后利润/底薪
                dto.setRateTimes(
                        payrollPrice != null && payrollPrice.compareTo(BigDecimal.ZERO) > 0 ?
                                dto.getNoPayRollRate().divide(payrollPrice, 6, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
                //盲盒售卖利润
                dto.setBlindBoxRate(levelDTO.getBlindBoxRate());
                //扣除盲盒后利润:毛利-盲盒利润
                dto.setNoBlindBoxRate(projectRate.subtract(dto.getBlindBoxRate()).compareTo(BigDecimal.ZERO) >= 0? projectRate.subtract(dto.getBlindBoxRate()) : BigDecimal.ZERO);
                // 评级
                dto.setRateLevel(levelDTO.getRateLevel());
                // 内部货值
                dto.setInnerPrice(levelDTO.getInnerPrice());
                // 货值工资比
                if(dto.getPayRollPrice() != null && dto.getPayRollPrice().compareTo(BigDecimal.ZERO) >= 0){
                    dto.setGoodPayRollRate(levelDTO.getInnerPrice().divide(dto.getPayRollPrice(), 6, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP));
                }else {
                    dto.setGoodPayRollRate(BigDecimal.ZERO);
                }
                // 绩效货值
                dto.setPerformanceGoodsPrice(performanceGoodsPrice);
//                //月度绩效完成进度 = 本月所有项目到货内部货值 / 本月绩效
//                dto.setMonthlyPerformanceGoodsRate(performanceGoodsPrice != null && performanceGoodsPrice.compareTo(BigDecimal.ZERO) > 0
//                        ? levelDTO.getMonthArriveInnerPrice().divide(performanceGoodsPrice, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
//                dto.setMonthArriveInnerPrice(levelDTO.getMonthArriveInnerPrice() != null ? levelDTO.getMonthArriveInnerPrice() : BigDecimal.ZERO);
//                // 本季度所有项目到货内部货值 / 本季度绩效
//                Map<String, Date> dateMap = DateUtil.getQuarterStartTimeAndEndTimeByDate(time);
//                Date quarterStartTime = dateMap.get("startTime");
//                Date quarterEndTime = dateMap.get("endTime");
//                List<MjProject> projects = projectMapper.selectHomeListByBusinessIdAndTime(dto.getBusinessId(), quarterStartTime, quarterEndTime);
//                BigDecimal totalInnerPrice = getTotalInnerPrice(projects,quarterStartTime,quarterEndTime);
//                List<MjBusinessTimePrice> mjBusinessTimePrices = businessTimePriceMapper.selectMsgByBusinessIdAndQuarterTime(dto.getBusinessId(), quarterStartTime, quarterEndTime);
//                BigDecimal performanceGoodsTotalPrice = BigDecimal.ZERO;
//                for (MjBusinessTimePrice mjBusinessTimePrice : mjBusinessTimePrices){
//                    performanceGoodsTotalPrice = performanceGoodsTotalPrice.add(mjBusinessTimePrice.getPerformanceGoodsPrice() != null ? mjBusinessTimePrice.getPerformanceGoodsPrice() : BigDecimal.ZERO);
//                }
//                dto.setQuarterPerformanceGoodsRate(performanceGoodsTotalPrice != null && performanceGoodsTotalPrice.compareTo(BigDecimal.ZERO) > 0 ? totalInnerPrice.divide(performanceGoodsTotalPrice, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
//
//                //年度绩效完成进度 = 本年度到货百分比 * 本年度绩效
//                List<MjProject> yearProjectList = projectMapper.selectProjectListByYearTimeAndBusinessId(time,dto.getBusinessId());
//                //计算年度内部货值
//                BigDecimal yearTotalInnerPrice = getYearTotalInnerPrice(yearProjectList, time);
//                //计算年度商务绩效
//                BigDecimal yearPerForMance = businessTimePriceMapper.selectYearPerForMance(dto.getBusinessId(), time);
//                if (yearPerForMance != null && yearPerForMance.compareTo(BigDecimal.ZERO) > 0){
//                    dto.setYearPerformanceGoodsRate(yearTotalInnerPrice != null && yearTotalInnerPrice.compareTo(BigDecimal.ZERO) > 0
//                            ? yearTotalInnerPrice.divide(yearPerForMance, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
//                } else {
//                    dto.setYearPerformanceGoodsRate(BigDecimal.ZERO);
//                }
            }
//            getInnerPerformance2(tables.getList(),arriveTime,time);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, HomeProjectMsg> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<HomeBusinessListDTO> list = businessPersonMapper.selectHomeListByTime(time, businessArr, area,targetId);
            for (HomeBusinessListDTO dto : list){
                // 底薪
                BigDecimal basicSalary = BigDecimal.ZERO;
                // 绩效
                BigDecimal performanceGoodsPrice = BigDecimal.ZERO;
                MjBusinessTimePrice businessTimePrice = businessTimePriceMapper.selectMsgByBusinessIdAndTime(dto.getBusinessId(), time);
                if (businessTimePrice != null){
                    basicSalary = businessTimePrice.getBasicSalary() != null ? businessTimePrice.getBasicSalary() : BigDecimal.ZERO;
                    performanceGoodsPrice = businessTimePrice.getPerformanceGoodsPrice() != null ? businessTimePrice.getPerformanceGoodsPrice() : BigDecimal.ZERO;
                }
                dto.setBasicSalary(basicSalary);
                // 工资
                BigDecimal payrollPrice = payrollMapper.selectPayPriceByUsername(dto.getBusinessName(), time, time);
                dto.setPayRollPrice(payrollPrice);

                List<MjProject> mjProjects = projectMapper.selectHomeListByBusinessIdAndTime(dto.getBusinessId(), time, time);
                BusinessLevelDTO levelDTO = businessManageService.getBusinessOrder(dto.getBusinessId(), mjProjects, payrollPrice,time, 4, blindOnePrice);
                // 毛利
                BigDecimal projectRate = levelDTO.getExpectGrossProfit();
                dto.setProjectRate(projectRate);
                // 去工资后利润：毛利-工资
                dto.setNoPayRollRate(levelDTO.getNoPayPriceProfit());
                // 利润倍数：去工资后利润 / 底薪
                dto.setRateTimes(
                        payrollPrice != null && payrollPrice.compareTo(BigDecimal.ZERO) > 0 ?
                                dto.getNoPayRollRate().divide(payrollPrice, 6, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
                // 盲盒售卖利润
                dto.setBlindBoxRate(levelDTO.getBlindBoxRate());
                // 扣除盲盒后利润:毛利 - 盲盒利润
                dto.setNoBlindBoxRate(projectRate.subtract(dto.getBlindBoxRate()).compareTo(BigDecimal.ZERO) >= 0? projectRate.subtract(dto.getBlindBoxRate()) : BigDecimal.ZERO);
                // 评级
                dto.setRateLevel(levelDTO.getRateLevel());
                // 内部货值
                dto.setInnerPrice(levelDTO.getInnerPrice());
                // 货值工资比
                if(dto.getPayRollPrice() != null && dto.getPayRollPrice().compareTo(BigDecimal.ZERO) >= 0){
                    dto.setGoodPayRollRate(levelDTO.getInnerPrice().divide(dto.getPayRollPrice(), 6, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP));
                }else {
                    dto.setGoodPayRollRate(BigDecimal.ZERO);
                }
                // 绩效货值
                dto.setPerformanceGoodsPrice(performanceGoodsPrice);
//                // 月度绩效完成进度 = 本月所有项目到货内部货值 / 本月绩效
//                dto.setMonthlyPerformanceGoodsRate(performanceGoodsPrice != null && performanceGoodsPrice.compareTo(BigDecimal.ZERO) > 0
//                        ? levelDTO.getMonthArriveInnerPrice().divide(performanceGoodsPrice, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
//                // 月到货内部货值
//                dto.setMonthArriveInnerPrice(levelDTO.getMonthArriveInnerPrice() != null ? levelDTO.getMonthArriveInnerPrice() : BigDecimal.ZERO);
//                // 本季度所有项目到货内部货值 / 本季度绩效
//                Map<String, Date> dateMap = DateUtil.getQuarterStartTimeAndEndTimeByDate(time);
//                Date quarterStartTime = dateMap.get("startTime");
//                Date quarterEndTime = dateMap.get("endTime");
//                List<MjProject> projects = projectMapper.selectHomeListByBusinessIdAndTime(dto.getBusinessId(), quarterStartTime, quarterEndTime);
//                BigDecimal totalInnerPrice = getTotalInnerPrice(projects,quarterStartTime,quarterEndTime);
//                List<MjBusinessTimePrice> mjBusinessTimePrices = businessTimePriceMapper.selectMsgByBusinessIdAndQuarterTime(dto.getBusinessId(), quarterStartTime, quarterEndTime);
//                BigDecimal performanceGoodsTotalPrice = BigDecimal.ZERO;
//                for (MjBusinessTimePrice mjBusinessTimePrice : mjBusinessTimePrices){
//                    performanceGoodsTotalPrice = performanceGoodsTotalPrice.add(mjBusinessTimePrice.getPerformanceGoodsPrice() != null ? mjBusinessTimePrice.getPerformanceGoodsPrice() : BigDecimal.ZERO);
//                }
//                dto.setQuarterPerformanceGoodsRate(performanceGoodsTotalPrice != null && performanceGoodsTotalPrice.compareTo(BigDecimal.ZERO) > 0 ? totalInnerPrice.divide(performanceGoodsTotalPrice, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
//
//                // 年度绩效完成进度 = 本年度到货百分比 * 本年度绩效
//                List<MjProject> yearProjectList = projectMapper.selectProjectListByYearTimeAndBusinessId(time,dto.getBusinessId());
//                // 计算年度内部货值
//                BigDecimal yearTotalInnerPrice = getYearTotalInnerPrice(yearProjectList, time);
//                // 计算年度商务绩效
//                BigDecimal yearPerForMance = businessTimePriceMapper.selectYearPerForMance(dto.getBusinessId(), time);
//                if (yearPerForMance != null && yearPerForMance.compareTo(BigDecimal.ZERO) > 0){
//                    dto.setYearPerformanceGoodsRate(yearTotalInnerPrice != null && yearTotalInnerPrice.compareTo(BigDecimal.ZERO) > 0
//                            ? yearTotalInnerPrice.divide(yearPerForMance, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
//                } else {
//                    dto.setYearPerformanceGoodsRate(BigDecimal.ZERO);
//                }
            }
//            getInnerPerformance2(list,arriveTime,time);

            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(list);
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }

    public APPResultMessage getBusinessPerformanceList(Date arriveTime, Integer pageNum, Integer pageSize, String businessIds, String area, Integer targetId) {
        String[] businessArr = null;
        if (StringUtil.isNotBlank(businessIds)){
            businessArr = businessIds.split(",");
        }
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
            List<HomeBusinessPerformanceListDTO> list = businessPersonMapper.selectHomeListByArriveTime(arriveTime, businessArr, area,targetId);
            PageInfo<HomeBusinessPerformanceListDTO> tables = new PageInfo<>(list);
            for (HomeBusinessPerformanceListDTO dto : tables.getList()){
                // 底薪
                BigDecimal basicSalary = BigDecimal.ZERO;
                MjBusinessTimePrice businessTimePrice = businessTimePriceMapper.selectMsgByBusinessIdAndTime(dto.getBusinessId(), arriveTime);
                if (businessTimePrice != null){
                    basicSalary = businessTimePrice.getBasicSalary() != null ? businessTimePrice.getBasicSalary() : BigDecimal.ZERO;
                }
                dto.setBasicSalary(basicSalary);
            }
            //绩效的计算
            getInnerPerformance(tables.getList(),arriveTime);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, HomeProjectMsg> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<HomeBusinessPerformanceListDTO> list = businessPersonMapper.selectHomeListByArriveTime(arriveTime, businessArr, area,targetId);
            for (HomeBusinessPerformanceListDTO dto : list){
                // 底薪
                BigDecimal basicSalary = BigDecimal.ZERO;
                MjBusinessTimePrice businessTimePrice = businessTimePriceMapper.selectMsgByBusinessIdAndTime(dto.getBusinessId(), arriveTime);
                if (businessTimePrice != null){
                    basicSalary = businessTimePrice.getBasicSalary() != null ? businessTimePrice.getBasicSalary() : BigDecimal.ZERO;
                }
                dto.setBasicSalary(basicSalary);

            }
            getInnerPerformance(list,arriveTime);
            APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
            ResultMessage resultMessage = ResultMessage.builder().setList(list);
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }
    /**
     * @author: ljl
     * @description: 计算每个商务的到货内部货值还有绩效完成度
     */
    public List<HomeBusinessPerformanceListDTO> getInnerPerformance(List<HomeBusinessPerformanceListDTO> businessList,Date arriveTime){
        if (arriveTime == null){

        } else {
            for (HomeBusinessPerformanceListDTO dto : businessList) {
                //月度所有项目到货内部货值
                BigDecimal monthArriveInnerPrice = BigDecimal.ZERO;
                //本月商务绩效
                BigDecimal monthPerformance = BigDecimal.ZERO;
                //季度所有项目到货内部货值
                BigDecimal quarterArriveInnerPrice = BigDecimal.ZERO;
                //季度商务绩效
                BigDecimal quarterPerformance = BigDecimal.ZERO;
                //年度所有项目到货内部货值
                BigDecimal yearArriveInnerPrice = BigDecimal.ZERO;
                //所选月份的总内部货值
                BigDecimal innerPrice = BigDecimal.ZERO;

                List<BusinessPerfomanceDTO> businessPerfomanceDTOS = projectMapper.selectArriveProjectList(arriveTime, dto.getBusinessId());
                if (CollectionUtils.isNotEmpty(businessPerfomanceDTOS)){
                    for (BusinessPerfomanceDTO businessPerfomanceDTO : businessPerfomanceDTOS) {
                        //每个项目的内部货值
                        BigDecimal projectInnerPrice = BigDecimal.ZERO;
                        List<MjOrder> orders = orderMapper.selectListByProjectId(businessPerfomanceDTO.getId());
                        if (CollectionUtils.isNotEmpty(orders)){
                            for (MjOrder order : orders) {
                                //先不找子订单，等查到父订单再计算
                                if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                                    //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                                    if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                                        List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                                        if (CollUtil.isNotEmpty(childOrderList)){
                                            for (TreeOrderListDTO childOrder : childOrderList) {
                                                projectInnerPrice = projectInnerPrice.add(childOrder.getInnerPrice() != null ? childOrder.getInnerPrice() : BigDecimal.ZERO);
                                                //所选月份的总内部货值
                                                innerPrice = innerPrice.add(childOrder.getInnerPrice() != null ? childOrder.getInnerPrice() : BigDecimal.ZERO);
                                            }
                                        }
                                    } else {
                                        projectInnerPrice = projectInnerPrice.add(order.getInnerPrice() != null ? order.getInnerPrice() : BigDecimal.ZERO);
                                        //所选月份的总内部货值
                                        innerPrice = innerPrice.add(order.getInnerPrice() != null ? order.getInnerPrice() : BigDecimal.ZERO);
                                    }
                                }
                            }
                        }
                        //每个项目的到货内部货值
                        if (businessPerfomanceDTO.getMonthArriveRate() != null){
                            BigDecimal bigDecimal = projectInnerPrice.compareTo(BigDecimal.ZERO) == 0
                                    ? BigDecimal.ZERO : projectInnerPrice.multiply(businessPerfomanceDTO.getMonthArriveRate()).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP);
                            monthArriveInnerPrice = monthArriveInnerPrice.add(bigDecimal);
                        }
                    }
                }

                //所选月份的到货内部货值
                dto.setMonthArriveInnerPrice(monthArriveInnerPrice);
                //所选月份的内部货值
                dto.setInnerPrice(innerPrice);
                //签单月份的绩效
                MjBusinessTimePrice businessTimePrice = businessTimePriceMapper.selectMsgByBusinessIdAndTime(dto.getBusinessId(), arriveTime);
                if (businessTimePrice != null && businessTimePrice.getPerformanceGoodsPrice() != null){
                    monthPerformance = businessTimePrice.getPerformanceGoodsPrice();
                    dto.setPerformanceGoodsPrice(monthPerformance);
                }
                //得出月度绩效比
                dto.setMonthlyPerformanceGoodsRate(monthPerformance != null && monthPerformance.compareTo(BigDecimal.ZERO) > 0
                        ? monthArriveInnerPrice.divide(monthPerformance, 6, BigDecimal.ROUND_HALF_UP)
                        .multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);

                // 本季度绩效
                Map<String, Date> dateMap = DateUtil.getQuarterStartTimeAndEndTimeByDate(arriveTime);
                Date quarterStartTime = dateMap.get("startTime");
                Date quarterEndTime = dateMap.get("endTime");
                List<BusinessPerfomanceDTO> quarterProjectList = projectMapper.selectQuarterArriveProjectList(quarterStartTime, quarterEndTime, dto.getBusinessId());
                if (CollectionUtils.isNotEmpty(quarterProjectList)){
                    for (BusinessPerfomanceDTO businessPerfomanceDTO : quarterProjectList) {
                        //每个项目的内部货值
                        BigDecimal projectInnerPrice = BigDecimal.ZERO;
                        List<MjOrder> orders = orderMapper.selectListByProjectId(businessPerfomanceDTO.getId());
                        if (CollectionUtils.isNotEmpty(orders)){
                            for (MjOrder order : orders) {
                                //先不找子订单，等查到父订单再计算
                                if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                                    //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                                    if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                                        List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                                        if (CollUtil.isNotEmpty(childOrderList)){
                                            for (TreeOrderListDTO childOrder : childOrderList) {
                                                projectInnerPrice = projectInnerPrice.add(childOrder.getInnerPrice() != null ? childOrder.getInnerPrice() : BigDecimal.ZERO);
                                            }
                                        }
                                    } else {
                                        projectInnerPrice = projectInnerPrice.add(order.getInnerPrice() != null ? order.getInnerPrice() : BigDecimal.ZERO);
                                    }
                                }
                            }
                        }
                        //每个项目的到货内部货值
                        if (businessPerfomanceDTO.getMonthArriveRate() != null){
                            BigDecimal bigDecimal = projectInnerPrice.compareTo(BigDecimal.ZERO) == 0
                                    ? BigDecimal.ZERO : projectInnerPrice.multiply(businessPerfomanceDTO.getMonthArriveRate()).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP);
                            quarterArriveInnerPrice = quarterArriveInnerPrice.add(bigDecimal);
                        }
                    }
                }
                //季度的绩效
                List<MjBusinessTimePrice> mjBusinessTimePrices = businessTimePriceMapper.selectMsgByBusinessIdAndQuarterTime(dto.getBusinessId(), quarterStartTime, quarterEndTime);
                if (CollectionUtils.isNotEmpty(mjBusinessTimePrices)){
                    for (MjBusinessTimePrice mjBusinessTimePrice : mjBusinessTimePrices){
                        quarterPerformance = quarterPerformance.add(mjBusinessTimePrice.getPerformanceGoodsPrice() != null ? mjBusinessTimePrice.getPerformanceGoodsPrice() : BigDecimal.ZERO);
                    }
                }
                //季度的绩效比
                dto.setQuarterPerformanceGoodsRate(quarterPerformance != null && quarterPerformance.compareTo(BigDecimal.ZERO) > 0
                        ? quarterArriveInnerPrice.divide(quarterPerformance, 6, BigDecimal.ROUND_HALF_UP)
                        .multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);

                //季度到货内部货值
                dto.setQuarterArriveInnerPrice(quarterArriveInnerPrice);
                //年度
                List<BusinessPerfomanceDTO> yearProjectList = projectMapper.selectYearProjectList(arriveTime, dto.getBusinessId());
                if (CollectionUtils.isNotEmpty(yearProjectList)){
                    for (BusinessPerfomanceDTO businessPerfomanceDTO : yearProjectList) {
                        //每个项目的内部货值
                        BigDecimal projectInnerPrice = BigDecimal.ZERO;
                        List<MjOrder> orders = orderMapper.selectListByProjectId(businessPerfomanceDTO.getId());
                        if (CollectionUtils.isNotEmpty(orders)){
                            for (MjOrder order : orders) {
                                //先不找子订单，等查到父订单再计算
                                if (ObjUtil.isNotNull(order) && ObjUtil.isNull(order.getParentId())){
                                    //筛选有子订单的列表，除了核价卖货应收用父订单的，其他都用子订单累加
                                    if ( ObjUtil.isNotNull(order.getIsHasChild()) && order.getIsHasChild() == 2){
                                        List<TreeOrderListDTO> childOrderList = orderMapper.selectChildOrder(order.getId());
                                        if (CollUtil.isNotEmpty(childOrderList)){
                                            for (TreeOrderListDTO childOrder : childOrderList) {
                                                projectInnerPrice = projectInnerPrice.add(childOrder.getInnerPrice() != null ? childOrder.getInnerPrice() : BigDecimal.ZERO);
                                            }
                                        }
                                    } else {
                                        projectInnerPrice = projectInnerPrice.add(order.getInnerPrice() != null ? order.getInnerPrice() : BigDecimal.ZERO);
                                    }
                                }
                            }
                        }
                        //每个项目的到货内部货值
                        if (businessPerfomanceDTO.getMonthArriveRate() != null){
                            BigDecimal bigDecimal = projectInnerPrice.compareTo(BigDecimal.ZERO) == 0
                                    ? BigDecimal.ZERO : projectInnerPrice.multiply(businessPerfomanceDTO.getMonthArriveRate()).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP);
                            yearArriveInnerPrice = yearArriveInnerPrice.add(bigDecimal);
                        }
                    }
                }
                //年度绩效
                BigDecimal yearPerForMance = businessTimePriceMapper.selectYearPerForMance(dto.getBusinessId(), arriveTime);
                if (yearPerForMance != null && yearPerForMance.compareTo(BigDecimal.ZERO) > 0){
                    dto.setYearPerformanceGoodsRate(yearArriveInnerPrice != null && yearArriveInnerPrice.compareTo(BigDecimal.ZERO) > 0
                            ? yearArriveInnerPrice.divide(yearPerForMance, 6, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
                    dto.setYearArriveInnerPrice(yearArriveInnerPrice);
                } else {
                    dto.setYearArriveInnerPrice(BigDecimal.ZERO);
                    dto.setYearPerformanceGoodsRate(BigDecimal.ZERO);
                }
            }
        }
        return businessList;
    }



    /**
     * @author: zhy
     * @description: 计算内部货值
     * @create: 2023年12月01日 13:18
     * @return
     */
    public BigDecimal getTotalInnerPrice(List<MjProject> projects,Date startTime,Date endTime){
        BigDecimal innerPrice = BigDecimal.ZERO;
        if (projects != null && projects.size() > 0){

            for (MjProject project : projects){
                BigDecimal projectInnerPrice = BigDecimal.ZERO;
                List<MjOrder> orders = orderMapper.selectListByProjectId(project.getId());
                if (orders != null && orders.size() > 0){
                    for (MjOrder order : orders){
//                        innerPrice = innerPrice.add(order.getInnerPrice() != null ? order.getInnerPrice() : BigDecimal.ZERO);
                        //每个项目的内部货值
                        projectInnerPrice = projectInnerPrice.add(order.getInnerPrice() != null ? order.getInnerPrice() : BigDecimal.ZERO);
                    }
                }
                if (startTime != null && endTime != null){
                    //得到每一个项目的到货百分比
                    BigDecimal quarterArriveRate = projectBatchMapper.selectQuarterArriveRate(startTime, endTime, project.getId());
                    if (quarterArriveRate != null){
                        BigDecimal bigDecimal = projectInnerPrice.compareTo(BigDecimal.ZERO) == 0
                                ? BigDecimal.ZERO : projectInnerPrice.multiply(quarterArriveRate).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP);
                        innerPrice = innerPrice.add(bigDecimal);
                    }
                }
            }
        }
        return innerPrice;
    }

    public BigDecimal getYearTotalInnerPrice(List<MjProject> projects,Date time) {
        BigDecimal yearInnerPrice = BigDecimal.ZERO;
        if (projects != null && projects.size() > 0) {
            for (MjProject project : projects) {
                BigDecimal projectInnerPrice = BigDecimal.ZERO;
                List<MjOrder> orders = orderMapper.selectListByProjectId(project.getId());
                if (orders != null && orders.size() > 0) {
                    for (MjOrder order : orders) {
                        //每个项目的内部货值
                        projectInnerPrice = projectInnerPrice.add(order.getInnerPrice() != null ? order.getInnerPrice() : BigDecimal.ZERO);
                    }
                }
                if (time != null) {
                    //得到每一个项目的到货百分比
                    BigDecimal yearArriveRate = projectBatchMapper.selectYearArriveRate(time, project.getId());
                    if (yearArriveRate != null) {
                        BigDecimal bigDecimal = projectInnerPrice.compareTo(BigDecimal.ZERO) == 0
                                ? BigDecimal.ZERO : projectInnerPrice.multiply(yearArriveRate).multiply(new BigDecimal(0.01)).setScale(2, BigDecimal.ROUND_HALF_UP);
                        yearInnerPrice = yearInnerPrice.add(bigDecimal);
                    }
                }
            }
        }
        return yearInnerPrice;
    }


    public Map<String,PersonPreProfit> initMap (){
        HashMap<String,PersonPreProfit> map = new HashMap<>();
        map.put("缪安勤",new PersonPreProfit(new  BigDecimal(1023439), new BigDecimal(953893.36)));
        map.put("尹杰",new PersonPreProfit(new  BigDecimal(-27626), new BigDecimal(-27625.73)));
        map.put("姚国强",new PersonPreProfit(new  BigDecimal(179587), new BigDecimal(179587.04)));
        map.put("姜振宇",new PersonPreProfit(new  BigDecimal(62192), new BigDecimal(15343.24)));
        map.put("张靓",new PersonPreProfit(new  BigDecimal(958011), new BigDecimal(937077.348)));
        map.put("孙辉",new PersonPreProfit(new  BigDecimal(525060), new BigDecimal(488080.33)));
        map.put("陶聪",new PersonPreProfit(new  BigDecimal(746037), new BigDecimal(714596.8055)));
        map.put("冉江飞",new PersonPreProfit(new  BigDecimal(-67101), new BigDecimal(-67101.15)));
        map.put("张烨",new PersonPreProfit(new  BigDecimal(1054761), new BigDecimal(630023.72)));
        map.put("余欢",new PersonPreProfit(new  BigDecimal(-2793), new BigDecimal(-2793.1)));
        map.put("程渝",new PersonPreProfit(new  BigDecimal(802382), new BigDecimal(445559.1815)));
        map.put("陈竹青",new PersonPreProfit(new  BigDecimal(124485), new BigDecimal(106645.27)));
        map.put("曹陈阳",new PersonPreProfit(new  BigDecimal(-2533), new BigDecimal(-2532.7)));
        map.put("马梓翔",new PersonPreProfit(new  BigDecimal(162682), new BigDecimal(132182.19)));
        map.put("李天辰",new PersonPreProfit(new  BigDecimal(-9662), new BigDecimal(-9662.15)));
        map.put("伍兴宝",new PersonPreProfit(new  BigDecimal(-805), new BigDecimal(-804.6)));
        map.put("赵肖芬",new PersonPreProfit(new  BigDecimal(199966), new BigDecimal(162902.31)));
        map.put("陈锋锋",new PersonPreProfit(new  BigDecimal(100182), new BigDecimal(100182.11)));
        map.put("沈建康",new PersonPreProfit(new  BigDecimal(375546), new BigDecimal(281290.2831)));
        map.put("蒋涵玉",new PersonPreProfit(new  BigDecimal(-2006), new BigDecimal(2006.13)));
        map.put("曹恒一",new PersonPreProfit(new  BigDecimal(-8582), new BigDecimal(-8581.61)));
        map.put("赵杰",new PersonPreProfit(new  BigDecimal(-2224), new BigDecimal(-2224.14)));
        map.put("高紫薇",new PersonPreProfit(new  BigDecimal(-1499), new BigDecimal(-1498.85)));
        map.put("张厚远",new PersonPreProfit(new  BigDecimal(13606), new BigDecimal(13605.74)));
        map.put("周冰倩",new PersonPreProfit(new  BigDecimal(-3500), new BigDecimal(-3500)));
        map.put("乔长莉",new PersonPreProfit(new  BigDecimal(-483), new BigDecimal(-482.76)));
        map.put("李曾超",new PersonPreProfit(new  BigDecimal(-966), new BigDecimal(-965.52)));
        map.put("卞婷婷",new PersonPreProfit(new  BigDecimal(-12099), new BigDecimal(-12099.36)));
        map.put("张雨晨",new PersonPreProfit(new  BigDecimal(107125), new BigDecimal(107125.33)));
        map.put("慎小旗",new PersonPreProfit(new  BigDecimal(504166), new BigDecimal(504166.04)));
        map.put("李婷婷",new PersonPreProfit(new  BigDecimal(-700), new BigDecimal(-700)));
        map.put("靳彬",new PersonPreProfit(new  BigDecimal(-5702), new BigDecimal(-5702.38)));
        map.put("蒋海涛",new PersonPreProfit(new  BigDecimal(-533), new BigDecimal(-533.33)));
        map.put("陶玉婷",new PersonPreProfit(new  BigDecimal(7017), new BigDecimal(7017.32)));
        map.put("邓倩",new PersonPreProfit(new  BigDecimal(263807), new BigDecimal(189807.39)));
        map.put("周西平",new PersonPreProfit(new  BigDecimal(-5076), new BigDecimal(-5075.86)));
        map.put("尼冰洁",new PersonPreProfit(new  BigDecimal(-11658.81), new BigDecimal(-11658.81)));
        map.put("邵震",new PersonPreProfit(new  BigDecimal(-4344), new BigDecimal(-4343.54)));
        map.put("许潇桠",new PersonPreProfit(new  BigDecimal(-808), new BigDecimal(-807.72)));
        map.put("何洪阳",new PersonPreProfit(new  BigDecimal(-552), new BigDecimal(-551.72)));
        map.put("廉浩宏",new PersonPreProfit(new  BigDecimal(-1751), new BigDecimal(-1750.8)));
        map.put("姚晨龙",new PersonPreProfit(new  BigDecimal(-10353), new BigDecimal(-10352.87)));
        map.put("凌涛",new PersonPreProfit(new  BigDecimal(150176), new BigDecimal(140003.7909)));
        map.put("管伟",new PersonPreProfit(new  BigDecimal(-2455), new BigDecimal(-2455.17)));
        map.put("张星驰",new PersonPreProfit(new  BigDecimal(7281), new BigDecimal(7281.36)));
        map.put("马秋宇",new PersonPreProfit(new  BigDecimal(-1655), new BigDecimal(-1655.17)));
        map.put("徐睿",new PersonPreProfit(new  BigDecimal(-413), new BigDecimal(-413)));
        map.put("李晓缓",new PersonPreProfit(new  BigDecimal(-413), new BigDecimal(-413)));
        map.put("汪颖",new PersonPreProfit(new  BigDecimal(-15872), new BigDecimal(-15871.84)));
        map.put("孙玲",new PersonPreProfit(new  BigDecimal(28186), new BigDecimal(-19338.04)));
        map.put("杨云玮",new PersonPreProfit(new  BigDecimal(22184), new BigDecimal(189807.39)));
        map.put("李克红",new PersonPreProfit(new  BigDecimal(-18092), new BigDecimal(-26584.57)));
        map.put("蒙功林",new PersonPreProfit(new  BigDecimal(-4278), new BigDecimal(-4277.6)));
        map.put("艾雅琪",new PersonPreProfit(new  BigDecimal(-11951), new BigDecimal(-11951.02)));
        map.put("代梦园",new PersonPreProfit(new  BigDecimal(-8847), new BigDecimal(-8846.68)));
        map.put("王晶晶",new PersonPreProfit(new  BigDecimal(-10647), new BigDecimal(-10646.68)));
        map.put("王舒湛",new PersonPreProfit(new  BigDecimal(-9451), new BigDecimal(-9451.02)));
        map.put("李莉",new PersonPreProfit(new  BigDecimal(654140), new BigDecimal(103583.51)));
        map.put("王刚",new PersonPreProfit(new  BigDecimal(-6000), new BigDecimal(-6000)));
        map.put("王亚雷",new PersonPreProfit(new  BigDecimal(-2000), new BigDecimal(-2000)));
        map.put("秦涛",new PersonPreProfit(new  BigDecimal(-2807), new BigDecimal(-2807.14)));
        map.put("季莎琳",new PersonPreProfit(new  BigDecimal(-2483), new BigDecimal(-2482.78)));
        map.put("刘林林",new PersonPreProfit(new  BigDecimal(-2483), new BigDecimal(-2482.76)));
        map.put("杨丹蕾",new PersonPreProfit(new  BigDecimal(-1200), new BigDecimal(-1200.39)));
        map.put("张健",new PersonPreProfit(new  BigDecimal(-1655), new BigDecimal(-1655.17)));
        map.put("夏子君",new PersonPreProfit(new  BigDecimal(-3121), new BigDecimal(-3120.69)));
        map.put("周佳阳",new PersonPreProfit(new  BigDecimal(-3120), new BigDecimal(-3120.39)));
        map.put("周诗曼",new PersonPreProfit(new  BigDecimal(335614), new BigDecimal(189807.39)));
        map.put("邓倩",new PersonPreProfit(new  BigDecimal(-1126), new BigDecimal(-1126.44)));
        map.put("黄晓敏",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("陆俊杰",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("姚海忠",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("汪梁栋",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("刘洋",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("王翊瑄",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("苏洲",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("张纯",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("陈怡彤",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("汪琪",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("陈平溪",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("王大林",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("王沁",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("赵倩云",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));
        map.put("李俊余",new PersonPreProfit(BigDecimal.ZERO, BigDecimal.ZERO));


        return map;
    }
    public APPResultMessage getBusinessHomeMonth(Date time) {
        HomeBusinessDTO dto = new HomeBusinessDTO();

        // 各个区域已签单商务总人数
        int hasOrderNum1 = projectMapper.selectCountByTimeAndAreaId(time, 3);
        dto.setHasOrderBusinessTotalNumSH(hasOrderNum1);
        int hasOrderNum2 = projectMapper.selectCountByTimeAndAreaId(time, 4);
        dto.setHasOrderBusinessTotalNumCZ(hasOrderNum2);
        int hasOrderNum3 = projectMapper.selectCountByTimeAndAreaId(time, 5);
        dto.setHasOrderBusinessTotalNumCQ(hasOrderNum3);
        // 已开单的商务总人数
        dto.setHasOrderBusinessTotalNum(hasOrderNum1 + hasOrderNum2 + hasOrderNum3);

        // 各个区域开单总额
        BigDecimal hasOrderPrice1 = BigDecimal.ZERO;//上海
        BigDecimal hasOrderPrice2 = BigDecimal.ZERO;//常州
        BigDecimal hasOrderPrice3 = BigDecimal.ZERO;//重庆
        List<MjProject> projects = projectMapper.selectHomeListByTime(time);
        for (MjProject project : projects){
            List<MjOrder> orders = orderMapper.selectListByProjectId(project.getId());
            for (MjOrder order : orders){
                if (project.getAreaId().equals("3")){
                    hasOrderPrice1 = hasOrderPrice1.add(order.getTotalPrice() == null ? BigDecimal.ZERO : order.getTotalPrice());
                }
                if (project.getAreaId().equals("4")){
                    hasOrderPrice2 = hasOrderPrice2.add(order.getTotalPrice() == null ? BigDecimal.ZERO : order.getTotalPrice());
                }
                if (project.getAreaId().equals("5")){
                    hasOrderPrice3 = hasOrderPrice3.add(order.getTotalPrice() == null ? BigDecimal.ZERO : order.getTotalPrice());
                }
            }
        }
        dto.setOrderTotalPriceSH(hasOrderPrice1);
        dto.setOrderTotalPriceCZ(hasOrderPrice2);
        dto.setOrderTotalPriceCQ(hasOrderPrice3);

        // 开单总额
        dto.setOrderTotalPrice(dto.getOrderTotalPriceCQ().add(dto.getOrderTotalPriceSH()).add(dto.getOrderTotalPriceCZ()));


        // 各个区域商务总人数，该时间段在职的商务
        List<MjBusinessPerson> totalNumList1 = businessPersonMapper.selectCountByTimeAndArea(time, "上海");
        dto.setBusinessTotalNumSH(totalNumList1.size());
        List<MjBusinessPerson> totalNumList2 = businessPersonMapper.selectCountByTimeAndArea(time, "常州");
        dto.setBusinessTotalNumCZ(totalNumList2.size());
        List<MjBusinessPerson> totalNumList3 = businessPersonMapper.selectCountByTimeAndArea(time, "重庆");
        dto.setBusinessTotalNumCQ(totalNumList3.size());

        //商务总人数
//        int totalNum = businessPersonMapper.selectCountByTime(time);
        dto.setBusinessTotalNum(totalNumList1.size() + totalNumList2.size() + totalNumList3.size());

        // 评级人数
        Integer ratePerson1 = 0;
        Integer ratePerson2 = 0;
        Integer ratePerson3 = 0;
        Integer ratePerson4 = 0;
        //    等级P
        //    p小于1的时候，极差
        //    1<=p<3,差
        //    3<=p<5，良好
        //    p>=5，优秀
        for (MjBusinessPerson businessPerson : totalNumList1){
            BigDecimal rateLevel = getRateLevel(businessPerson.getId(), businessPerson.getUsername(), time);
            if (rateLevel.compareTo(new BigDecimal(1)) < 0){
                ratePerson4 = ratePerson4 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(1)) >= 0 && rateLevel.compareTo(new BigDecimal(3)) < 0){
                ratePerson3 = ratePerson3 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(3)) >= 0 && rateLevel.compareTo(new BigDecimal(5)) < 0){
                ratePerson2 = ratePerson2 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(5)) >= 0) {
                ratePerson1 = ratePerson1 + 1;
            }
        }
        for (MjBusinessPerson businessPerson : totalNumList2){
            BigDecimal rateLevel = getRateLevel(businessPerson.getId(), businessPerson.getUsername(), time);
            if (rateLevel.compareTo(new BigDecimal(1)) < 0){
                ratePerson4 = ratePerson4 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(1)) >= 0 && rateLevel.compareTo(new BigDecimal(3)) < 0){
                ratePerson3 = ratePerson3 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(3)) >= 0 && rateLevel.compareTo(new BigDecimal(5)) < 0){
                ratePerson2 = ratePerson2 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(5)) >= 0) {
                ratePerson1 = ratePerson1 + 1;
            }
        }
        for (MjBusinessPerson businessPerson : totalNumList3){
            BigDecimal rateLevel = getRateLevel(businessPerson.getId(), businessPerson.getUsername(), time);
            if (rateLevel.compareTo(new BigDecimal(1)) < 0){
                ratePerson4 = ratePerson4 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(1)) >= 0 && rateLevel.compareTo(new BigDecimal(3)) < 0){
                ratePerson3 = ratePerson3 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(3)) >= 0 && rateLevel.compareTo(new BigDecimal(5)) < 0){
                ratePerson2 = ratePerson2 + 1;
            }else if (rateLevel.compareTo(new BigDecimal(5)) >= 0) {
                ratePerson1 = ratePerson1 + 1;
            }
        }
        dto.setRatePerson1(ratePerson1);
        dto.setRatePerson2(ratePerson2);
        dto.setRatePerson3(ratePerson3);
        dto.setRatePerson4(ratePerson4);

        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setData(dto);
        successMessage.setResult(resultMessage);
        return successMessage;
    }


    public APPResultMessage doEditSomeProjectPrice(Integer projectId, BigDecimal innerPrice, BigDecimal actualValuePrice,
                                                   BigDecimal saleRealPrice,
                                                   BigDecimal estimatedSelfMediaCost, BigDecimal estimatedExternalMediaCost,
                                                   BigDecimal actualSelfMediaCost, BigDecimal actualExternalMediaCost) {
        if (actualValuePrice != null){
            projectMapper.updateSomePriceById(projectId, actualValuePrice);
        }
        BigDecimal estimatedCost = BigDecimal.ZERO;
        BigDecimal actualCost = BigDecimal.ZERO;
        if (innerPrice != null || saleRealPrice != null ||
                estimatedSelfMediaCost != null || estimatedExternalMediaCost != null
                || actualSelfMediaCost != null || actualExternalMediaCost != null){
            List<MjOrder> mjOrders = orderMapper.selectListByProjectId(projectId);
            if (estimatedSelfMediaCost != null || estimatedExternalMediaCost != null
                    || actualSelfMediaCost != null || actualExternalMediaCost != null){
                for (MjOrder order : mjOrders){
                    if (order.getParentId() == null){
                        // 查询出父订单
                        BigDecimal e1 = order.getEstimatedSelfMediaCost() != null ? order.getEstimatedSelfMediaCost() : BigDecimal.ZERO;
                        BigDecimal e2 = order.getEstimatedExternalMediaCost() != null ? order.getEstimatedExternalMediaCost() : BigDecimal.ZERO;
                        estimatedCost = estimatedSelfMediaCost != null && estimatedSelfMediaCost.compareTo(BigDecimal.ZERO) > 0 ? estimatedSelfMediaCost : e1
                                .add(estimatedExternalMediaCost != null && estimatedExternalMediaCost.compareTo(BigDecimal.ZERO) > 0 ? estimatedExternalMediaCost : e2);
                        BigDecimal a1 = order.getActualSelfMediaCost() != null ? order.getActualSelfMediaCost() : BigDecimal.ZERO;
                        BigDecimal a2 = order.getActualExternalMediaCost() != null ? order.getActualExternalMediaCost() : BigDecimal.ZERO;
                        actualCost = actualSelfMediaCost != null && actualSelfMediaCost.compareTo(BigDecimal.ZERO) > 0 ? actualSelfMediaCost : a1
                                .add(actualExternalMediaCost != null && actualExternalMediaCost.compareTo(BigDecimal.ZERO) > 0 ? actualExternalMediaCost : a2);
                    }
                }
            }
            // 只修改父订单，如果有子订单，进入后续的操作进行修改
            orderMapper.updateSomePriceByProjectId(projectId, innerPrice, saleRealPrice,
                    estimatedCost.compareTo(BigDecimal.ZERO) > 0 ? estimatedCost : null,
                    actualCost.compareTo(BigDecimal.ZERO) > 0 ? actualCost : null,
                    estimatedSelfMediaCost, estimatedExternalMediaCost,
                    actualSelfMediaCost, actualExternalMediaCost);
            for (MjOrder order : mjOrders){
                if (order.getIsHasChild() == 2){
                    orderManagerService.doCountSubOrder(order.getId());
                }
            }
        }
        return APPResultMessage.getSuccessMessage();
    }
}
